相关文章推荐
粗眉毛的蚂蚁  ·  python tkinter | ...·  4 天前    · 
憨厚的企鹅  ·  opencv imencode ...·  10 月前    · 
善良的烤地瓜  ·  pandas - How to split ...·  11 月前    · 

python tkinter界面切换

Python tkinter是Python中一个图形用户界面(GUI)库,它可以帮助您在Python中创建复杂的图形界面。如果您想在tkinter界面中切换,请使用以下代码:

import tkinter as tk
class MainWindow:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.button1 = tk.Button(self.frame, text = 'New Window', width = 25, command = self.new_window)
        self.button1.pack()
        self.frame.pack()
    def new_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = SecondWindow(self.newWindow)
class SecondWindow:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
        self.quitButton.pack()
        self.frame.pack()
    def close_windows(self):
        self.master.destroy()
def main(): 
    root = tk.Tk()
    app = MainWindow(root)
    root.mainloop()
if __name__ == '__main__':
    main()

在上面的代码中,我们使用了两个类:MainWindow和SecondWindow。MainWindow类是主窗口,它有一个按钮,单击该按钮将打开SecondWindow窗口。SecondWindow类是新窗口,它有一个退出按钮,单击该按钮将关闭窗口。

  •