简体   繁体   中英

Tkinter label go at the bottom of the frame

I have kind of a problem with my little Python application using Tkinter and the pack layout management. I don't understand why the labels go at the bottom of the frame.

Here is the code:

class App:

def __init__(self, master):

    frame = Frame(master)
    frame.pack()

    photo = PhotoImage(file="icone.gif")
    self.label = Label(image=photo)
    self.label.image = photo # keep a reference!
    self.label.pack()

    self.labelNet = Label(master, text='NetID:')
    self.labelNet.pack()

    self.netID = Entry(frame,width=20)
    self.netID.pack()

    self.password = Label(master, text='Password:')
    self.password.pack()

    self.password = Entry(frame, show="*",  width=20)
    self.password.pack()

    self.install = Button(frame, text="Activer", command=self.install)
    self.install.pack()

    self.uninstall = Button(frame, text="Désactiver", command=self.uninstall)
    self.uninstall.pack()

called by:

root = Tk()
root.title("Title")
root.geometry("200x280")
app = App(root)
root.mainloop()

The result:

Do you have any idea what the problem could be ?

Thanks !

self.label = Label(image=photo)
...
self.labelNet = Label(master, text='NetID:')
...
self.password = Label(master, text='Password:')

All of your labels either use master as their master, or you provided no master (in which case it defaults to the root window). All of your other widgets have the frame as their master. This effectively causes you to have a widget hierarchy like:

root
    frame
        entry
        entry
        button
        button
    label
    label
    label

You should make the Labels have the frame as their master too.

self.label = Label(frame, image=photo)
...
self.labelNet = Label(frame, text='NetID:')
...
self.password = Label(frame, text='Password:')

Or, alternatively, just don't have a frame at all, and make everything a direct child of the root.

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