The problem is that you are using place
to position your entries and the button but you use pack
for the labels. % default pack
positions the widgets aligned in column from top to bottom.
There are several ways to do this but you can follow the maxim of 'divide and conquer'. To align the label and the entries, a grid is usually used. You can use a container (a Frame
) that will contain the two Entry
and the two Label
and then position the Frame
appropriately.
Two observations:
-
You should not use to import the model:
from tkinter import *
You may not have big problems in your program being small, but I assure you that you can find unpleasant surprises when using extensive and complex libraries. Instead use something like:
import tkinter as tk
-
I do not advise you to use the widget instance and its positioning on the same line using grid
, place
or pack
, if you do:
btnSave=tk.Button(window,text="Save",command=Entryquestion,).place(x=130,y=200)
Now btnSave
is not an instance of tkinter.Button
but it is None
, the return of the place
method. If now you would like to use the variable btnSave
to change, for example, the text of the button could not. Instead, separate the lines:
btnSave=tk.Button(window,text="Save",command=Entryquestion,)
btnSave.place(x=130,y=200)
The code could be something like this:
import tkinter as tk
def Entryquestion():
pass
#Creando una ventanta principal
window=tk.Tk()
window.geometry("500x300+100+100")
window.title("Question Editor")
#Creamos un frame como contenedor
frame = tk.Frame(window)
#Creando un campo de texto para question
entryQuestion=tk.StringVar()
entryQuestion.set("")
txtQuestion=tk.Entry(frame,textvariable=entryQuestion)
txtQuestion.grid(row=0, column=1)
#Creando un campo de texto para answer
entryAnswer=tk.StringVar()
entryAnswer.set("")
txtAnswer=tk.Entry(frame,textvariable=entryAnswer)
txtAnswer.grid(row=2, column=1)
#Creando un label para el campo de texto "question"
labelQuestion = tk.Label(frame, text="Question", padx=10 )
labelQuestion.grid(row=0, column=0, sticky=tk.W)
#Creando un label para el campo de texto "answer"
labelAnswer = tk.Label(frame, text="Answer", padx=10 )
labelAnswer.grid(row=2, column=0, sticky=tk.W)
#Definimos un tamaño mínimo de la fila central delgrid para que quede un espacio entre cada entry y posicionamos el frame
frame.grid_rowconfigure(1, minsize=10)
frame.place(x=0,y=140)
#Creando un botón para guardar pregunta y respuesta
btnSave=tk.Button(window,text="Save question and answer",command=Entryquestion,font=("Agency FB",14))
btnSave.place(x=130,y=200)
#Iniciamos el mailoop
window.mainloop()
We get something like: