Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from tkinter import *
- class WelcomeFrame(Frame):
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.root = args[0]
- # content
- self.welcome_text = Label(self, text="Hello, this is my new application!", height=10, anchor="n")
- self.welcome_text.pack(padx=5, pady=5)
- # frame changer logic
- self.next_button = Button(self, text="Next", command=self.root.nextFrame)
- self.next_button.pack(side="right")
- class State0Frame(Frame):
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.root = args[0]
- # content
- self.welcome_text = Label(self, text="You currently see the page 2", height=10, anchor="n")
- self.welcome_text.pack(padx=5, pady=5)
- # frame changer logic
- self.next_button = Button(self, text="Next", command=self.root.nextFrame)
- self.next_button.pack(side="right")
- class State1Frame(Frame):
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.root = args[0]
- # content
- self.welcome_text = Label(self, text="And now, you see the last page", height=10, anchor="n")
- self.welcome_text.pack(padx=5, pady=5)
- # frame changer logic
- self.next_button = Button(self, text="Next", command=self.root.nextFrame)
- self.next_button.pack(side="right")
- def changeNextButton(self):
- "This is calld from the parent because this is the last Frame"
- self.next_button.configure(text="Kilépés", command=self.root.destroy)
- class App(Tk):
- def __init__(self):
- super().__init__()
- # just for test
- self.title("Teszt")
- self.minsize(230, 220)
- self.maxsize(230, 220)
- self.current_frame = None
- self.current_state = 0 # this will keep the current state of the Frames
- if not self.createFrames(): raise Exception("I could not create instances")
- self.setFrame(self.current_state)
- def createFrames(self) -> bool:
- "Creates and saves the different kind of Frames' instances"
- self.app_frames = []
- wanted_frames = [WelcomeFrame, State0Frame, State1Frame]
- for child in wanted_frames:
- self.app_frames.append(child) # this is currently a lil bit ugly but this is just for reprezenntation
- return True
- def setFrame(self, index_of_new_frame, last = False):
- "This sets the new Frame every time when you use the next button"
- if self.current_frame is not None: self.current_frame.pack_forget() # or grid or whatver
- self.current_frame = self.app_frames[index_of_new_frame](self)
- if last: self.current_frame.changeNextButton()
- 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
- def nextFrame(self):
- "This is the func what is called from 'outside'"
- c_app_frame = len(self.app_frames)
- next_frame = self.current_state + 1
- last_frame = False
- if next_frame < c_app_frame:
- self.current_state += 1
- if next_frame + 1 == c_app_frame: last_frame = True
- self.setFrame(next_frame, last_frame)
- else:
- pass # do something to notice the user
- if __name__ == "__main__":
- main_app = App()
- main_app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement