简体   繁体   English

遇到错误; 使用python的Tkinter GUI

[英]Encountered error; Tkinter GUI using python

I'm creating a simple Tkinter gui. 我正在创造一个简单的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. (除了使用'from Tkinter import *'引起的问题,以及显然无用的'do_nothing()'函数。

#/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. 您正在混合gridpack在同一主窗口中。 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. 因此,pack将调整窗口小部件以适应,网格将识别更改并尝试调整窗口小部件以适应,pack将识别更改并尝试调整窗口小部件以适应,...导致无限循环。

You can use pack and grid together in the same program, but you cannot use them to manage the same container. 您可以在同一程序中一起使用packgrid ,但不能使用它们来管理同一个容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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