TkinterのGUIデザイン:インターフェースとロジックの分離(3)-複数のページ



Tkinters Gui Design Separation Interface



知識のポイント:

tkinter.Frame.tkraise()関数を使用して、現在のtkinter.Frameのz軸の順序を改善し、複数のtkinter.Frameの可視性を切り替えることができるようにします。



この記事は以下に基づいています:win7 + python34

1





3

4

5

画像
import matplotlib matplotlib.use('TkAgg') 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg from matplotlib.figure import Figure import tkinter as tk from tkinter import ttk LARGE_FONT= ('Verdana', 12) class Application(tk.Tk): ''' Multi-page test program Interface and logic separation ''' def __init__(self): super().__init__() self.iconbitmap(default='kankan_01.ico') self.wm_title('Multi-page test program') container = tk.Frame(self) container.pack(side='top', fill='both', expand = True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = {} for F in (StartPage, PageOne, PageTwo, PageThree): frame = F(container, self) self.frames[F] = frame frame.grid(row=0, column=0, sticky='nsew') # The positions of the four pages are grid(row=0, column=0), the positions overlap, and only the top one is visible! ! self.show_frame(StartPage) def show_frame(self, cont): frame = self.frames[cont] frame.tkraise() # Switch to improve the z-axis order of the current tk.Frame (make it visible)! ! This statement is the finishing touch of this program class StartPage(tk.Frame): '''Homepage''' def __init__(self, parent, root): super().__init__(parent) label = tk.Label(self, text='Here is the homepage', font=LARGE_FONT) label.pack(pady=10,padx=10) button1 = ttk.Button(self, text='Go to the first page', command=lambda: root.show_frame(PageOne)).pack() button2 = ttk.Button(self, text='Go to the second page', command=lambda: root.show_frame(PageTwo)).pack() button3 = ttk.Button(self, text='Go to the drawing page', command=lambda: root.show_frame(PageThree)).pack() class PageOne(tk.Frame): '''The first page''' def __init__(self, parent, root): super().__init__(parent) label = tk.Label(self, text='This is the first page', font=LARGE_FONT) label.pack(pady=10,padx=10) button1 = ttk.Button(self, text='Back to homepage', command=lambda: root.show_frame(StartPage)).pack() button2 = ttk.Button(self, text='Go to the second page', command=lambda: root.show_frame(PageTwo)).pack() class PageTwo(tk.Frame): '''The second page''' def __init__(self, parent, root): super().__init__(parent) label = tk.Label(self, text='This is the second page', font=LARGE_FONT) label.pack(pady=10,padx=10) button1 = ttk.Button(self, text='Back to homepage', command=lambda: root.show_frame(StartPage)).pack() button2 = ttk.Button(self, text='Go to the first page', command=lambda: root.show_frame(PageOne)).pack() class PageThree(tk.Frame): '''The third page''' def __init__(self, parent, root): super().__init__(parent) tk.Label(self, text='This is the drawing page', font=LARGE_FONT).pack(pady=10,padx=10) button1 = ttk.Button(self, text='Back to homepage', command=lambda: root.show_frame(StartPage)).pack() fig = Figure(figsize=(5,5), dpi=100) a = fig.add_subplot(111) a.plot([1,2,3,4,5,6,7,8],[5,6,1,3,8,9,3,5]) canvas = FigureCanvasTkAgg(fig, self) canvas.show() canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True) toolbar = NavigationToolbar2TkAgg(canvas, self) toolbar.update() canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True) if __name__ == '__main__': # Instantiate Application app = Application() # Main message loop: app.mainloop()
画像 この記事は、Luo Bingのブログ、元のリンクから複製されています:http://www.cnblogs.com/hhh5460/p/5170744.html、転載が必要な場合は原作者にご連絡ください