简体   繁体   中英

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:

  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.

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

In you program you have been using grid for aligning. I would say that you replace with place instead.

place will allow you to set a definite x and y coordinate for the widget and it would be easy to use.

If I alter your code accordingly, I can show you the code (after alteration) and the image of the 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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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