Tkinter窗口仍然没有出现

时间:2018-11-13 09:47:42

标签: python tkinter

我正在尝试创建一个简单的tkinter GUI,并已在线阅读以在代码末尾添加.mainloop()以使窗口出现,但该窗口仍未出现。

没有错误消息,只是

Process finished with exit code 0

我已附上我的代码。

非常感谢您的帮助

def window():
    global FPS
    global maxFPS
    root = Tk()
    root.title('Camera Tracker')
    root.geometry('500x300')

def quitfunc():

    quitm=Tk()
    quitm.title('Quit')
    quitm.geometry('200x100')

    yesbutton=Button(quitm,text='Yes',command=quit)
    nobutton =Button(quitm,text='No',command=quitm.destroy)

    yesbutton.place(x=50,y=60)
    nobutton.place(x=130,y=60)
    reassure = Label(quitm,text='Are you sure you want to quit?')
    reassure.place(x=17,y=20)

    quitm.mainloop()

sbview = Label(root, text=FPS)
sbview.place(y=50, x=50)

def FPScallback(self):
    global FPS
    FPS = round(sb.get())
    if 10 > FPS < 18 or 29 < FPS:
        sbview.config(fg='orange')
    elif FPS < 10:
        sbview.config(fg='red')
    else:
        sbview.config(fg='green')
    sbview.config(text=FPS)

quitbutton = Button(root,command=quitfunc,text='Quit')
quitbutton.pack()

sb = ttk.Scale(root, from_=0, to=maxFPS, command=FPScallback, orient=HORIZONTAL)
sb.place(y=100, x=100)
sb.set(FPS)

root.mainloop()

先谢谢了

1 个答案:

答案 0 :(得分:0)

如果从函数内部创建root,则在函数退出时将不可用。在我的示例中,我将在全局范围内创建根以及一些全局变量。

要询问用户是否要退出,使用标准库messagebox更容易。如果您想捕获任何关闭应用程序的尝试(例如 ALT-F4 ),则应重新搜索root.protocol("WM_DELETE_WINDOW", do_exit),当应用程序要退出时,该do_exit运行from tkinter import * from tkinter import ttk from tkinter import messagebox # Quit dialog root = Tk() # Create root in the global scope root.title('Camera Tracker') # so the functions can find it root.geometry('500x300') maxFPS = 50 # Initiate variables in the global scope FPS = 25 sbview = Label(root, text=FPS) sbview.place(y=50, x=50) def quitfunc(): result = messagebox.askyesno('Quit', 'Are you sure you want to quit?') if result: root.destroy() def FPScallback(self): global FPS FPS = round(sb.get()) if 10 > FPS < 18 or 29 < FPS: sbview.config(fg='orange') elif FPS < 10: sbview.config(fg='red') else: sbview.config(fg='green') sbview.config(text=FPS) sb = ttk.Scale(root, from_=0, to=maxFPS, command=FPScallback, orient=HORIZONTAL) sb.place(y=100, x=100) sb.set(FPS) quitbutton = Button(root, command=quitfunc, text='Quit') quitbutton.pack() root.mainloop() 函数手段。

File -> Templates -> Recording