Python: Using label in Tkinter

0

for a program I want to place two label "Question" and "Answer" next to two text boxes. As shown in the following image:

The problem is that with the following code:

from tkinter import *

#Creando una ventanta principal
window=Tk()
window.geometry("500x300+100+100")
window.title("Question Editor")

#Creando un campo de texto para question
Entryquestion=StringVar()
Entryquestion.set("")
txtquestion=Entry(window,textvariable=Entryquestion).place(x=50,y=140)

#Creando un campo de texto para answer
Entryanswer=StringVar()
Entryanswer.set("")
txtquestion=Entry(window,textvariable=Entryanswer).place(x=50,y=170)
#Creando un label para el campo de texto "question"
var1 = StringVar()
label = Label( window, textvariable=var1, padx=10 )

var1.set("Question")
label.pack()

#Creando un label para el campo de texto "answer"
var2 = StringVar()
label = Label( window, textvariable=var2, padx=10 )
var2.set("Answer")
label.pack()

#Creando un botón para guardar pregunta y respuesta
btnSave=Button(window,text="Save question and answer",command=Entryquestion,font=("Agency FB",14)).place(x=130,y=200)

It gives me the following result:

How can I get the visual configuration of the first image I've shown?

Thank you very much in advance.

    
asked by Mr. Baldan 12.04.2017 в 01:04
source

1 answer

2

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:

    
answered by 12.04.2017 / 03:32
source