简体   繁体   中英

Encountered error; Tkinter GUI using python

I'm creating a simple Tkinter gui. However, something appears to be going haywire. Nothing is actually being 'pack'ed to the frame. Can anyone spot what I've done wrong? (Other than the issues caused by using 'from Tkinter import *', and the apparently un-useful 'do_nothing()' function.

#/usr/bin/python
from Tkinter import *

class gui:
    def __init__(self, parent):
        f = Frame(parent, width=300, height=500)
        f.pack(padx=30, pady=15)

        self.label = Label(f, text="Japanese Trainer")
        self.label.pack(side=TOP, padx=10, pady=12)

        self.txtlbl = Entry(f, justify=CENTER, text="", font=("Calibri", 15, "bold"), width=37)
        self.txtlbl.pack()
        self.txtlbl.grid(row=1, rowspan=2, sticky=E, pady=10, padx=40)

        self.button0 = Button(f, text="Kana Trainer", width=20, command=self.do_nothing)
        self.button0.pack()
        self.button0.grid(row=3, rowspan=2, sticky=W, pady=10, padx=40)

        self.button1 = Button(f, text="Vocab Trainer", width=20, command=self.do_nothing)
        self.button1.pack()
        self.button1.grid(row=3, rowspan=2, sticky=E, pady=10, padx=40)

    def do_nothing(self):
        self.txtlbl.delete(0, END)
        self.txtlbl.insert(END, "Command did nothing...")


root = Tk()
root.title('Eg.')
app = gui(root)

root.mainloop()

You are mixing grid and pack in the same master window. You can't do that. Each one will potentially resize the widgets they manage, and each will respond to resizes in the widgets they manage. So, pack will resize the widgets to fit, grid will recognize the change and try to resize widgets to fit, pack will recognize the change and try to resize widgets to fit, ... resulting in an endless loop.

You can use pack and grid together in the same program, but you cannot use them to manage the same container.

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