Advertisement
silvertank

Untitled

Apr 2nd, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from tkinter import *
  3.  
  4. class WelcomeFrame(Frame):
  5.     def __init__(self, *args, **kwargs):
  6.         super().__init__(*args, **kwargs)
  7.         self.root = args[0]
  8.  
  9.         # content
  10.         self.welcome_text = Label(self, text="Hello, this is my new application!", height=10, anchor="n")
  11.         self.welcome_text.pack(padx=5, pady=5)
  12.  
  13.         # frame changer logic
  14.         self.next_button = Button(self, text="Next", command=self.root.nextFrame)
  15.         self.next_button.pack(side="right")
  16.  
  17. class State0Frame(Frame):
  18.     def __init__(self, *args, **kwargs):
  19.         super().__init__(*args, **kwargs)
  20.         self.root = args[0]
  21.  
  22.         # content
  23.         self.welcome_text = Label(self, text="You currently see the page 2", height=10, anchor="n")
  24.         self.welcome_text.pack(padx=5, pady=5)
  25.  
  26.         # frame changer logic
  27.         self.next_button = Button(self, text="Next", command=self.root.nextFrame)
  28.         self.next_button.pack(side="right")
  29.  
  30. class State1Frame(Frame):
  31.     def __init__(self, *args, **kwargs):
  32.         super().__init__(*args, **kwargs)
  33.         self.root = args[0]
  34.  
  35.         # content
  36.         self.welcome_text = Label(self, text="And now, you see the last page", height=10, anchor="n")
  37.         self.welcome_text.pack(padx=5, pady=5)
  38.  
  39.         # frame changer logic
  40.         self.next_button = Button(self, text="Next", command=self.root.nextFrame)
  41.         self.next_button.pack(side="right")
  42.    
  43.     def changeNextButton(self):
  44.         "This is calld from the parent because this is the last Frame"
  45.         self.next_button.configure(text="Kilépés", command=self.root.destroy)
  46.  
  47. class App(Tk):
  48.     def __init__(self):
  49.         super().__init__()
  50.  
  51.         # just for test
  52.         self.title("Teszt")
  53.         self.minsize(230, 220)
  54.         self.maxsize(230, 220)
  55.  
  56.         self.current_frame = None
  57.         self.current_state = 0 # this will keep the current state of the Frames
  58.  
  59.         if not self.createFrames(): raise Exception("I could not create instances")
  60.         self.setFrame(self.current_state)
  61.  
  62.     def createFrames(self) -> bool:
  63.         "Creates and saves the different kind of Frames' instances"
  64.         self.app_frames = []
  65.         wanted_frames = [WelcomeFrame, State0Frame, State1Frame]
  66.  
  67.         for child in wanted_frames:
  68.             self.app_frames.append(child) # this is currently a lil bit ugly but this is just for reprezenntation
  69.  
  70.         return True
  71.  
  72.     def setFrame(self, index_of_new_frame, last = False):
  73.         "This sets the new Frame every time when you use the next button"
  74.         if self.current_frame is not None: self.current_frame.pack_forget() # or grid or whatver
  75.  
  76.         self.current_frame = self.app_frames[index_of_new_frame](self)
  77.         if last: self.current_frame.changeNextButton()
  78.  
  79.         self.current_frame.pack() # or grid or whatever, but remind if you change this geometry manager you will have to change the forgeting method too
  80.  
  81.     def nextFrame(self):
  82.         "This is the func what is called from 'outside'"
  83.         c_app_frame = len(self.app_frames)
  84.         next_frame = self.current_state + 1
  85.         last_frame = False
  86.  
  87.         if next_frame < c_app_frame:
  88.             self.current_state += 1
  89.             if next_frame + 1 == c_app_frame: last_frame = True
  90.  
  91.             self.setFrame(next_frame, last_frame)
  92.         else:
  93.             pass # do something to notice the user
  94.  
  95. if __name__ == "__main__":
  96.     main_app = App()
  97.     main_app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement