使用ttk.Notebook可实现实现多页面切换
直接上代码:
import tkinter as tk
from tkinter import ttk
from tkinter import END
class App:
def __init__(self,master):
self.notebook = ttk.Notebook(master)
self.frame1 = tk.Frame(master)
self.frame2 = tk.Frame(master)
self.label = tk.Label(self.frame1, text='Python',font=('华文行楷',12))
self.label.grid()
self.entry1 = tk.Entry(self.frame1,width = 35)
self.entry1.grid(row = 0,column=1)
self.bu1 = tk.Button(self.frame1,text = '请点击1',command = self.bu1)
self.bu1.grid(row=0,column=2)
self.list1 = tk.Listbox(self.frame1,font=('微软雅黑',12),width=38,heigh=8)
self.list1.grid(row=1,columnspan=5)
self.bu2 = tk.Button(self.frame2, text='请点击2',command=self.bu2)
self.bu2.grid()
self.list2 = tk.Listbox(self.frame2,font=('微软雅黑',12),width=38,heigh=8)
self.list2.grid(row=1,columnspan=5)
self.notebook.add(self.frame1, text='test1')
self.notebook.add(self.frame2, text='test2')
self.notebook.pack(padx=10, pady=5, fill=tk.BOTH, expand=True)
def bu1(self):
self.list1.insert(END,self.entry1.get()+'人生苦短,我用Python')
self.list1.see(END)
self.list1.update()
def bu2(self):
self.list2.insert(END,'Python使我更加强大!')
self.list2.see(END)
self.list2.update()
if __name__ == '__main__':
root = tk.Tk()
root.title('notebook_test')
# root.iconbitmap('fa.ico') #设置左上角小图标
root.geometry('400x200+200+100')
# root.resizable(0, 0) #设置窗口不可变
App(root)
root.mainloop()