简体   繁体   中英

Why can't I use the grid function?

I cannot use the grid function in Tkinter for Python 3 on Mac. Whenever I try to run it, IDLE stops responding and I have to quit it, but when I use the .pack() function it runs well.

from tkinter import *



class MainWindow:
    def __init__(self):
        window = Tk()
        frame = Frame(window, width=600, height=800)
        frame.pack(expand=True)
        btframe = Frame(window)
        btframe.pack(side=BOTTOM, expand=True)

        self.intro = Label(frame, text="Welcome to your personal email client", font =("Courier", 20, "bold"))
        self.name = Label(frame, text="\nName",foreground="red", font=("Helvetica",16,"bold"))
        self.password = Label(frame, text="\nPassword", foreground="blue", font=("Helvetica",16,"bold"))

        self.entry1 = Entry(frame)
        self.entry2 = Entry(frame)

        self.intro.pack()
        self.name.grid(row=1, column=2)
        self.entry1.grid(row=1, column=3)
        self.password.grid(row=3, column=2)
        self.entry2.grid(row=3, column=3)


        self.submitB = Button(btframe, text="Submit", command= self.display)
        self.quitB = Button(btframe, text="Quit", command=window.destroy)
        self.submitB.pack()
        self.quitB.pack()
        window.mainloop()

    def display(self):
        print("Submitted Succesfully")


MainWindow()

It is because you are trying to use grid and pack on widgets that share the same parent. Specifically, you are using pack for self.intro and grid for self.name but they both share a common parent of frame .

Use one or the other, not both. You can use both, but only when the widgets don't share a common parent.

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