简体   繁体   中英

Tkinter self.grid making widgets invisible

I am making a username/password confirmation using tkinter, and I've run into a problem where the widgets are no longer showing.

"""
Password entry/confirmation class
"""
from tkinter import *

class Menu:
    def __init__(self,master):

        frame = Frame(master)
        frame.pack()

        title_username = Label(master, text="User Name")
        title_username.grid(row=0, column=0)

        title_password = Label(master, text="Password")
        title_password.grid(row=1, column=0)

        global username
        username = Entry(master, bd=5)
        username.grid(row=0, column=1)

        global password
        password = Entry(master, bd=5)
        password.grid(row=1, column=1)

        confirm_username = Button(master, text="Done",
                                  fg="black",command=self.get_username)
        confirm_username.grid(row=0, column=2)

        confirm_password = Button(master, text="Done",
                                  fg="black",command=self.get_password)
        confirm_password.grid(row=1, column=2)

    def show_all(self):
        self.root.update()
        self.root.deiconify()

    def get_username(self):
        global username
        username = username.get()
        print(username)

    def get_password(self):
        global password
        password = password.get()
        print(password)

root = Tk()
app = Menu(root)
root.mainloop()

The widgets have not been showing since I've been using grid() instead of pack() . (global needs to be declared twice on each variable for some reason, but this is not an issue)

You are putting the initial frame (variable frame ) in master , and you are using pack . The remaining widgets are also going in master , and you are using grid . This typically results in your program locking up because grid will try to arrange the widgets, pack will notice they've changed and try to rearrange the widgets, grid will notice they've changed and try to rearrange the widgets, and so on.

My guess is, the original intent of the code was to put title_username , title_password , etc. in frame rather than master since there's no other apparent use for frame .

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