在tkinter中切换窗口的方法:
一、使用Frame控件进行切换实例:
from tkinter import *
def b():
f1.pack_forget()
f2.pack()
win = Tk()
f1 = Frame(win)
f1.pack()
f2 = Frame(win)
f2.pack()
f2.pack_forget()
b1 = Button(f1,text="切换窗口",command=b).pack()
l1 = Label(f2,text="₍ᐢ..ᐢ₎♡").pack()
win.mainloop()
其中pack_forget()是隐藏控件的作用,pack()是显示控件,整个代码的作用是:单机按钮的时候隐藏f1布局,显示f2布局
第二种方法:
文件法
创建一个名为cs1.py 文件,在文件中输入:
def main():
import tkinter as tk
def b():
print("")
win = tk.Tk()
b1 = tk.Button(win, text="切换窗口", command=b).pack()
l1 = tk.Label(win, text="( ᗜ ‸ ᗜ ) ").pack()
win.mainloop()
在main.py中输入:
from tkinter import *
import cs1 as m
def b():
m.main()
win = Tk()
f1 = Frame(win)
f1.pack()
f2 = Frame(win)
f2.pack()
f2.pack_forget()
b1 = Button(f1,text="切换窗口",command=b).pack()
l1 = Label(f2,text="₍ᐢ..ᐢ₎♡").pack()
win.mainloop()
运行main.py文件,单机按钮即可出现一个新的窗口,如果创建的窗口较多,可以使用多线程实现(病毒……)
还有其他的,方法很多。