简体   繁体   English

为什么我的按钮没有正确对齐 python TKinter

[英]Why aren't my buttons properly aligned with python TKinter

I am creating a password manager which includes some buttons, but for some reason these buttons aren't aligning properly, could someone help out?我正在创建一个包含一些按钮的密码管理器,但由于某种原因,这些按钮没有正确对齐,有人可以帮忙吗? Here is the code i've done usint Tkinter for these buttons:这是我为这些按钮使用 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)

which makes my program look like this:这使我的程序看起来像这样: 在此处输入图像描述

Any general tips or helpful links for how to make a nicer GUI would be appreciated as well, as I have been struggling with that.任何关于如何制作更好的 GUI 的一般提示或有用的链接也将不胜感激,因为我一直在努力解决这个问题。

As @acw1668 said if you don't specify row in grid() , it will take the next available row.正如@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)

By the way it is a good idea to use different names for the different buttons/labels.顺便说一句,为不同的按钮/标签使用不同的名称是个好主意。

I have been trying various method of aligning the widgets of tkinter in the program window lately and well I have found a better working solution to this.我最近一直在尝试在程序 window 中对齐 tkinter 的小部件的各种方法,而且我找到了一个更好的工作解决方案。

In you program you have been using grid for aligning.在您的程序中,您一直在使用grid进行对齐。 I would say that you replace with place instead.我会说你用place代替。

place will allow you to set a definite x and y coordinate for the widget and it would be easy to use. place将允许您为小部件设置明确的 x 和 y 坐标,并且易于使用。

If I alter your code accordingly, I can show you the code (after alteration) and the image of the output.如果我相应地更改您的代码,我可以向您展示代码(更改后)和 output 的图像。


Code (After Alteration)代码(修改后)

# 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)

The Output Screen Output屏

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM