[英]Why aren't my buttons properly aligned with python TKinter
我正在创建一个包含一些按钮的密码管理器,但由于某种原因,这些按钮没有正确对齐,有人可以帮忙吗? 这是我为这些按钮使用 Tkinter 完成的代码:
btn = Button(window, text="Exit Securely", command=exit)
btn.grid(column=2)
btn = Button(window, text="Add Entry", command=addEntry)
btn.grid(column=1)
btn = Button(window, text="Generate", command=run)
btn.grid(column=0)
lbl = Label(window, text="Website")
lbl.grid(row=3, column=0, padx=80)
lbl = Label(window, text="Username")
lbl.grid(row=3, column=1, padx=80)
lbl = Label(window, text="password")
lbl.grid(row=3, column=2, padx=80)
任何关于如何制作更好的 GUI 的一般提示或有用的链接也将不胜感激,因为我一直在努力解决这个问题。
正如@acw1668 所说,如果您未在grid()
中指定row
,它将占用下一个可用行。
# Code to make this example work:
from tkinter import *
def addEntry():pass
def run():pass
window = Tk()
# Added `row=0` for each one of them
btn = Button(window, text="Exit Securely", command=exit)
btn.grid(row=0, column=2)
btn = Button(window, text="Add Entry", command=addEntry)
btn.grid(row=0, column=1)
btn = Button(window, text="Generate", command=run)
btn.grid(row=0, column=0)
# Changed the row to 1 for all of them
lbl = Label(window, text="Website")
lbl.grid(row=1, column=0, padx=80)
lbl = Label(window, text="Username")
lbl.grid(row=1, column=1, padx=80)
lbl = Label(window, text="password")
lbl.grid(row=1, column=2, padx=80)
顺便说一句,为不同的按钮/标签使用不同的名称是个好主意。
我最近一直在尝试在程序 window 中对齐 tkinter 的小部件的各种方法,而且我找到了一个更好的工作解决方案。
在您的程序中,您一直在使用grid
进行对齐。 我会说你用place
代替。
place
将允许您为小部件设置明确的 x 和 y 坐标,并且易于使用。
如果我相应地更改您的代码,我可以向您展示代码(更改后)和 output 的图像。
代码(修改后)
# Code to make this example work:
from tkinter import *
def addEntry():pass
def run():pass
window = Tk()
# Adding geometry ettig.
window.geometry('500x500')
btn = Button(window, text="Exit Securely", command=exit)
btn.place(x=410, y=20)
btn = Button(window, text="Add Entry", command=addEntry)
btn.place(x=210, y=20)
btn = Button(window, text="Generate", command=run)
btn.place(x=10, y=20)
lbl = Label(window, text="Website")
lbl.place(x=10, y=50)
lbl = Label(window, text="Username")
lbl.place(x=210, y=50)
lbl = Label(window, text="password")
lbl.place(x=410, y=50)
Output屏
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.