Tkinter 中标签旁边的左对齐输入框?

时间:2021-03-31 22:48:13

标签: python-3.x tkinter

所以我想知道为什么我的输入框显示在中心。在我得到名字和姓氏的标签说明之前,它们是正确对齐的。我在第 0 列上有标签,在第 1 列上有输入框,但是它们并没有直接排列在一起。如果有人可以帮助我提供非常基本的解决方案和语言,那我显然是一个初学者..

Heres what the GUI looks like

root = Tk()
root.title("Login Information Help")
root.configure(background='black')
root.geometry("1920x1080")

def button_click(number):
    textbox.delete(0, END)
    textbox.insert(0, number)

instructions = Label(root, text="Please fill in all information below to recieve student account details.", bg="black", fg="white", font="times, 20") #Create instructions line
first_name_text = Label(root, text="Enter First Name", bg="black", fg="white", font="times, 20" ) #enter first name text
inputbox1 = Entry(root, width=30, font="times, 20") #Create input boxes
last_name_text = Label(root, text="Enter Last Name", bg="black", fg="white", font="times, 20" ) #enter last name text
inputbox2 = Entry(root, width=30, font="Times, 20") #Create input boxes
button_1 = Button(root, text="Get Info", padx=45, pady=5, bg="#808080", font="times, 15", command=lambda: button_click("1")) #Define Buttons

instructions.grid(row=0, column=0) #Define Instructions variables (labels)
first_name_text.grid(row=1, column=0, pady=0, sticky=W) #position first name text
inputbox1.grid(row=1, column=1, padx=10) #Postion inputbox 1
last_name_text.grid(row=2, column=0, sticky=W) #position last name text
inputbox2.grid(row=2, column=1, padx=10, pady=10, sticky=W) #Postion inputbox 2
button_1.grid(row=3, column=0, sticky=W) #Put Buttons on Screen

root.mainloop()```

2 个答案:

答案 0 :(得分:0)

第一行必须跨越 2 列。

instructions.grid(row=0, column=0, columnspan=2) 

对每个小部件使用 sticky=EW 来填充该列。

添加 root.columnconfigure(0, weight=0) 以不展开第一列。

答案 1 :(得分:0)

首先这个方法使用 pack 因为这是我想出的(更多解释如下)这里是我的代码:

from tkinter import Tk, Label, Entry, Button, Frame

root = Tk()
root.title("Login Information Help")
root.configure(background='black')
root.geometry("1920x1080")

def button_click(number):
    textbox.delete(0, END)
    textbox.insert(0, number)

row0 = Frame(root, bg='black')
row0.pack(side='top', fill='y', anchor='nw')

row1 = Frame(root, bg='black')
row1.pack(side='top', fill='y', anchor='nw')

row2 = Frame(root, bg='black')
row2.pack(side='top', fill='y', anchor='nw')

row3 = Frame(root, bg='black')
row3.pack(side='top', fill='y', anchor='nw')

instructions = Label(row0, text="Please fill in all information below to recieve student account details.", bg="black", fg="white", font="times, 20") #Create instructions line
first_name_text = Label(row1, text="Enter First Name", bg="black", fg="white", font="times, 20" ) #enter first name text
inputbox1 = Entry(row1, width=30, font="times, 20") #Create input boxes
last_name_text = Label(row2, text="Enter Last Name", bg="black", fg="white", font="times, 20" ) #enter last name text
inputbox2 = Entry(row2, width=30, font="Times, 20") #Create input boxes
button_1 = Button(row3, text="Get Info", padx=45, pady=5, bg="#808080", font="times, 15", command=lambda: button_click("1")) #Define Buttons

instructions.pack(side='left')
first_name_text.pack(side='left')
inputbox1.pack(side='left')
last_name_text.pack(side='left')
inputbox2.pack(side='left', padx=2)
button_1.pack(side='left')

root.mainloop()

说明:在顶部创建的那些框架像行一样堆叠,所以每当我使用 side 参数向每个框架添加内容时,它们都会像列一样被添加到那里 ,那个 padx 是为了让对齐看起来更好。此外,我建议更好地组织代码并开始学习课程,因为它们在处理 tkinter 时有很大帮助。

所以基本上让我们以其中一帧为例。 row1:当使用 .pack 添加并使用一侧(本例中为左侧)时,小部件会排成一排,但是将它们排成一排并不容易,这就是原因在 row1 的正下方还有一个名为 row2 的框架,由于 top 侧与小部件的 left 侧类似,因此它被直接打包在下方,并且这些框架仅在以下位置展开有小部件(或者也许对于那个扩展它必须有填充的参数,不知道)无论如何有一个美好的一天和实验