菜鸟IT的博客 >> Python
用tkinter.ttk.Notebook组件实现多界面切换,但不销毁窗口,多个程序同事运行,而且程序还不死机。我用2个for循环输出,界面切换。程序双方都同时运行
# encoding:utf-8
# -*- coding:utf-8 -*-
# ——————————————————————
import tkinter
import tkinter.ttk
import time
import threading
# ——————————————————————
def Page1_Doing():
t1 = threading.Thread(target=Page1_Doing_SonThread, args=())
t1.setDaemon(True)
t1.start()
def Page1_Doing_SonThread():
for i in range(1,50+1):
print("我是界面1")
Text1.insert(tkinter.END,"我是界面1,第"+str(i)+"次呼叫!"+"\n")
time.sleep(1)
# ——————————————————
def Page2_Doing():
t2 = threading.Thread(target=Page2_Doing_SonThread, args=())
t2.setDaemon(True)
t2.start()
def Page2_Doing_SonThread():
for i in range(1, 50 + 1):
print("我是界面2")
Text2.insert(tkinter.END, "我是界面2,第" + str(i) + "次呼叫!" + "\n")
time.sleep(1)
# ——————————————————————
root = tkinter.Tk()
root.title('notebook_test')
# root.iconbitmap('fa.ico') #设置左上角小图标
root.geometry('800x500+200+100')
# root.resizable(0, 0) #设置窗口不可变
# ———————————————————————
Notebook_QieHuan=tkinter.ttk.Notebook(root)
Frame1=tkinter.Frame(root)
Frame2=tkinter.Frame(root)
# ——————————————
Button1=tkinter.Button(Frame1,text="界面1点击",command=Page1_Doing)
Button1.pack()
Text1=tkinter.Text(Frame1)
Text1.pack()
# ——————————————
Button2=tkinter.Button(Frame2,text="界面2点击",command=Page2_Doing)
Button2.pack()
Text2=tkinter.Text(Frame2)
Text2.pack()
# ———————————————————————
Notebook_QieHuan.add(Frame1, text='页签1')
Notebook_QieHuan.add(Frame2, text='页签2')
Notebook_QieHuan.pack(padx=10, pady=5, fill=tkinter.BOTH, expand=True)
# ———————————————————————
root.mainloop()
菜鸟IT博客[2022.05.02-11:15] 访问:358