为什么我有一个空白的窗口?

2024-07-01 06:56:34 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行代码时:

from Tkinter import *
import thread
class App:
    def __init__(self, master):
        print master

        def creatnew():

            admin=Tk()
            lab=Label(admin,text='Workes')
            lab.pack()
            admin.minsize(width=250, height=250)
            admin.maxsize(width=250, height=250)
            admin.configure(bg='light green')
            admin.mainloop()
        def other():
            la=Label(master,text='other')
            la.pack()
            bu=Button(master,text='clicks',command=lambda: thread.start_new_thread(creatnew,()))
            bu.pack()
        other()

Admin = Tk()

Admin.minsize(width=650, height=500)
Admin.maxsize(width=650, height=500)
app = App(Admin)
Admin.mainloop()

我得到了第二个tkinter窗口,但它是一个白色的空白屏幕,使两个程序都没有响应。 有什么想法吗


Tags: textimportmasterappadmindeflabwidth
1条回答
网友
1楼 · 发布于 2024-07-01 06:56:34

不要使用线程。它混淆了Tkinter主回路。对于第二个窗口,创建一个Toplevel窗口。在

您的代码只需少量修改:

from Tkinter import *
# import thread # not needed

class App:
    def __init__(self, master):
        print master

        def creatnew(): # recommend making this an instance method

            admin=Toplevel() # changed Tk to Toplevel
            lab=Label(admin,text='Workes')
            lab.pack()
            admin.minsize(width=250, height=250)
            admin.maxsize(width=250, height=250)
            admin.configure(bg='light green')
            # admin.mainloop() # only call mainloop once for the entire app!
        def other(): # you don't need define this as a function
            la=Label(master,text='other')
            la.pack()
            bu=Button(master,text='clicks',command=creatnew) # removed lambda+thread
            bu.pack()
        other() # won't need this if code is not placed in function

Admin = Tk()

Admin.minsize(width=650, height=500)
Admin.maxsize(width=650, height=500)
app = App(Admin)
Admin.mainloop()

相关问题 更多 >

    热门问题