Guest User

Untitled

a guest
Jun 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import Tkinter as tk
  2.  
  3. class BaseFrame(tk.Frame):
  4. """An abstract base class for the frames that sit inside PythonGUI.
  5.  
  6. Args:
  7. master (tk.Frame): The parent widget.
  8. controller (PythonGUI): The controlling Tk object.
  9.  
  10. Attributes:
  11. controller (PythonGUI): The controlling Tk object.
  12.  
  13. """
  14.  
  15. def __init__(self, master, controller):
  16. tk.Frame.__init__(self, master)
  17. self.controller = controller
  18. self.grid()
  19. self.create_widgets()
  20.  
  21. def create_widgets(self):
  22. """Create the widgets for the frame."""
  23. raise NotImplementedError
  24.  
  25.  
  26. class ExecuteFrame(BaseFrame):
  27. """The application home page.
  28.  
  29. Attributes:
  30. new_button (tk.Button): The button to switch to HomeFrame.
  31.  
  32. """
  33.  
  34. def create_widgets(self):
  35. """Create the base widgets for the frame."""
  36. self.new_button = tk.Button(self,
  37. anchor=tk.W,
  38. command=lambda: self.controller.show_frame(HomeFrame),
  39. padx=5,
  40. pady=5,
  41. text="Home")
  42. self.new_button.grid(padx=5, pady=5, sticky=tk.W+tk.E)
  43.  
  44.  
  45. class HomeFrame(BaseFrame):
  46. """The application home page.
  47.  
  48. Attributes:
  49. new_button (tk.Button): The button to switch to ExecuteFrame.
  50.  
  51. """
  52.  
  53. def create_widgets(self):
  54. """Create the base widgets for the frame."""
  55. self.new_button = tk.Button(self,
  56. anchor=tk.W,
  57. command=lambda: self.controller.show_frame(ExecuteFrame),
  58. padx=5,
  59. pady=5,
  60. text="Execute")
  61. self.new_button.grid(padx=5, pady=5, sticky=tk.W+tk.E)
  62.  
  63.  
  64. class PythonGUI(tk.Tk):
  65. """The main window of the GUI.
  66.  
  67. Attributes:
  68. container (tk.Frame): The frame container for the sub-frames.
  69. frames (dict of tk.Frame): The available sub-frames.
  70.  
  71. """
  72.  
  73. def __init__(self):
  74. tk.Tk.__init__(self)
  75. self.title("Python GUI")
  76. self.create_widgets()
  77. self.resizable(0, 0)
  78.  
  79. def create_widgets(self):
  80. """Create the widgets for the frame."""
  81. # Frame Container
  82. self.container = tk.Frame(self)
  83. self.container.grid(row=0, column=0, sticky=tk.W+tk.E)
  84.  
  85. # Frames
  86. self.frames = {}
  87. for f in (HomeFrame, ExecuteFrame): # defined subclasses of BaseFrame
  88. frame = f(self.container, self)
  89. frame.grid(row=2, column=2, sticky=tk.NW+tk.SE)
  90. self.frames[f] = frame
  91. self.show_frame(HomeFrame)
  92.  
  93. def show_frame(self, cls):
  94. """Show the specified frame.
  95.  
  96. Args:
  97. cls (tk.Frame): The class of the frame to show.
  98.  
  99. """
  100. self.frames[cls].tkraise()
  101.  
  102. if __name__ == "__main__":
  103. app = PythonGUI()
  104. app.mainloop()
  105. exit()
Add Comment
Please, Sign In to add comment