Advertisement
Uno-Dan

Untitled

Oct 14th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. import os
  2. import numpy as np
  3. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
  4. from matplotlib.figure import Figure
  5. from tkinter import Tk, PhotoImage
  6. from tkinter.ttk import Frame, Label, Button
  7.  
  8. LARGE_FONT = ("Verdana", 12)
  9.  
  10.  
  11. class GeoffGUI(Tk):
  12.     def __init__(self, *args, **kwargs):
  13.         super().__init__(*args, **kwargs)
  14.         self.rowconfigure(0, weight=1)
  15.         self.columnconfigure(0, weight=1)
  16.  
  17.         container = Frame(self)
  18.  
  19.         container.grid(sticky='nsew')
  20.         container.rowconfigure(0, weight=1)
  21.         container.columnconfigure(0, weight=1)
  22.         container.grid()
  23.  
  24.         self.frames = {}
  25.         for obj in (StartPage, PageOne, PageTwo, PageThree):
  26.             self.frames[obj.__name__] = obj(container, self)
  27.             self.frames[obj.__name__].grid(row=0, column=0, sticky="nsew")
  28.  
  29.         self.show_frame('StartPage')
  30.  
  31.     def show_frame(self, cont):
  32.         self.frames[cont].tkraise()
  33.  
  34.  
  35. class StartPage(Frame):
  36.     def __init__(self, parent, controller):
  37.         super().__init__(parent)
  38.         Label(self, text="Start Page", font=LARGE_FONT).grid(padx=10, pady=10)
  39.         Button(self, text="Visit Page 1", command=lambda: controller.show_frame('PageOne')).grid()
  40.         Button(self, text="Visit Page 2", command=lambda: controller.show_frame('PageTwo')).grid()
  41.         Button(self, text="Visit Page 3 (Matplotlib)", command=lambda: controller.show_frame('PageThree')).grid()
  42.  
  43.  
  44. class PageOne(Frame):
  45.     def __init__(self, parent, controller):
  46.         super().__init__(parent)
  47.         Label(self, text="Page 1", font=LARGE_FONT).grid(padx=10, pady=10)
  48.         Button(self, text="Back to Home", command=lambda: controller.show_frame('StartPage')).grid()
  49.         Button(self, text="Visit Page 2", command=lambda: controller.show_frame('PageTwo')).grid()
  50.         Button(self, text="Visit Page 3 (Matplotlib)", command=lambda: controller.show_frame('PageThree')).grid()
  51.  
  52.  
  53. class PageTwo(Frame):
  54.     def __init__(self, parent, controller):
  55.         super().__init__(parent)
  56.         Label(self, text="Page 2", font=LARGE_FONT).grid(padx=10, pady=10)
  57.         Button(self, text="Back to Home", command=lambda: controller.show_frame('StartPage')).grid()
  58.         Button(self, text="Visit Page 1", command=lambda: controller.show_frame('PageOne')).grid()
  59.         Button(self, text="Visit Page 3 (Matplotlib)", command=lambda: controller.show_frame('PageThree')).grid()
  60.  
  61.  
  62. class PageThree(Frame):
  63.     def __init__(self, parent, controller):
  64.         super().__init__(parent)
  65.         Label(self, text="Matplotlib", font=LARGE_FONT).grid(padx=10, pady=10)
  66.         Button(self, text="Back to Home", command=lambda: controller.show_frame('StartPage')).grid()
  67.  
  68.         fig = Figure(figsize=(5, 4), dpi=100)
  69.         t = np.arange(0, 3, .01)
  70.         fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))
  71.  
  72.         canvas = FigureCanvasTkAgg(fig, master=self)
  73.         canvas.draw()
  74.         canvas.get_tk_widget().grid()
  75.  
  76.         toolbar = NavigationToolbar2Tk(canvas, self)
  77.         toolbar.update()
  78.         canvas.get_tk_widget().pack()
  79.  
  80.  
  81. app = GeoffGUI()
  82. app.title("A simple GUI program")
  83.  
  84. # if "nt" == os.name:
  85. #     pass
  86. # else:
  87. #     icon = PhotoImage(file='clienticon.png')
  88. #     app.tk.call('wm', 'iconphoto', app._w, icon)
  89.  
  90. app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement