You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. try:
  2. import tkinter as tk # python 3
  3. from tkinter import font as tkfont # python 3
  4. except ImportError:
  5. import Tkinter as tk # python 2
  6. import tkFont as tkfont # python 2
  7. class SampleApp(tk.Tk):
  8. def __init__(self, *args, **kwargs):
  9. tk.Tk.__init__(self, *args, **kwargs)
  10. self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
  11. # the container is where we'll stack a bunch of frames
  12. # on top of each other, then the one we want visible
  13. # will be raised above the others
  14. container = tk.Frame(self)
  15. container.pack(side="top", fill="both", expand=True)
  16. container.grid_rowconfigure(0, weight=1)
  17. container.grid_columnconfigure(0, weight=1)
  18. self.frames = {}
  19. for F in (StartPage, PageOne, PageTwo):
  20. page_name = F.__name__
  21. frame = F(parent=container, controller=self)
  22. self.frames[page_name] = frame
  23. # put all of the pages in the same location;
  24. # the one on the top of the stacking order
  25. # will be the one that is visible.
  26. frame.grid(row=0, column=0, sticky="nsew")
  27. self.show_frame("StartPage")
  28. def show_frame(self, page_name):
  29. '''Show a frame for the given page name'''
  30. frame = self.frames[page_name]
  31. frame.tkraise()
  32. class StartPage(tk.Frame):
  33. def __init__(self, parent, controller):
  34. tk.Frame.__init__(self, parent)
  35. self.controller = controller
  36. label = tk.Label(self, text="This is the start page", font=controller.title_font)
  37. label.pack(side="top", fill="x", pady=10)
  38. button1 = tk.Button(self, text="Go to Page One",
  39. command=lambda: controller.show_frame("PageOne"))
  40. button2 = tk.Button(self, text="Go to Page Two",
  41. command=lambda: controller.show_frame("PageTwo"))
  42. button1.pack()
  43. button2.pack()
  44. class PageOne(tk.Frame):
  45. def __init__(self, parent, controller):
  46. tk.Frame.__init__(self, parent)
  47. self.controller = controller
  48. label = tk.Label(self, text="This is page 1", font=controller.title_font)
  49. label.pack(side="top", fill="x", pady=10)
  50. button = tk.Button(self, text="Go to the start page",
  51. command=lambda: controller.show_frame("StartPage"))
  52. button.pack()
  53. class PageTwo(tk.Frame):
  54. def __init__(self, parent, controller):
  55. tk.Frame.__init__(self, parent)
  56. self.controller = controller
  57. label = tk.Label(self, text="This is page 2", font=controller.title_font)
  58. label.pack(side="top", fill="x", pady=10)
  59. button = tk.Button(self, text="Go to the start page",
  60. command=lambda: controller.show_frame("StartPage"))
  61. button.pack()
  62. if __name__ == "__main__":
  63. app = SampleApp()
  64. app.mainloop()