Skip to content

Instantly share code, notes, and snippets.

@i2x
Created October 17, 2023 17:37
Show Gist options
  • Select an option

  • Save i2x/fd3d8619b5176211e17dede318f1da29 to your computer and use it in GitHub Desktop.

Select an option

Save i2x/fd3d8619b5176211e17dede318f1da29 to your computer and use it in GitHub Desktop.
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Game with Different Components")
# Keep track of the current scene
self.current_scene = 0
# Define the scenes
self.scenes = [
self.create_text_scene(),
self.create_checkbox_scene(),
self.create_slider_scene()
]
# Create Next and Back buttons
self.next_btn = tk.Button(self, text="Next", command=self.next_scene)
self.back_btn = tk.Button(self, text="Back", command=self.previous_scene)
# Display the first scene
self.scenes[self.current_scene].pack(pady=20, padx=10, fill=tk.BOTH, expand=True)
self.next_btn.pack(side=tk.RIGHT, padx=10)
self.back_btn.pack(side=tk.LEFT, padx=10)
def create_text_scene(self):
frame = tk.Frame(self)
text_widget = tk.Text(frame, wrap=tk.WORD, font=("Arial", 12))
text_widget.insert(tk.END, "Type here for Scene 1")
text_widget.pack(fill=tk.BOTH, expand=True)
return frame
def create_checkbox_scene(self):
frame = tk.Frame(self)
options = ["Option 1", "Option 2", "Option 3"]
self.check_vars = [tk.IntVar() for _ in options]
for i, option in enumerate(options):
chk = tk.Checkbutton(frame, text=option, variable=self.check_vars[i])
chk.pack(anchor=tk.W, padx=10, pady=5)
return frame
def create_slider_scene(self):
frame = tk.Frame(self)
slider = tk.Scale(frame, from_=0, to=100, orient=tk.HORIZONTAL, label="Slide for Scene 3")
slider.pack(pady=20)
return frame
def next_scene(self):
"""Navigate to the next scene."""
self.scenes[self.current_scene].pack_forget()
self.current_scene = (self.current_scene + 1) % len(self.scenes)
self.scenes[self.current_scene].pack(pady=20, padx=10, fill=tk.BOTH, expand=True)
def previous_scene(self):
"""Navigate to the previous scene."""
self.scenes[self.current_scene].pack_forget()
self.current_scene = (self.current_scene - 1) % len(self.scenes)
self.scenes[self.current_scene].pack(pady=20, padx=10, fill=tk.BOTH, expand=True)
if __name__ == "__main__":
app = App()
app.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment