Python Forum
[Tkinter] help me to sokve this problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] help me to sokve this problem
#1
Smile 
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

# Import necessary modules for animations
import time
from itertools import cycle

class MainWindow:
    def __init__(self, master):
        self.master = master
        self.master.geometry("800x600")
        self.master.title("Physics Simulation")
        self.master.configure(bg='white')

        self.style = ttk.Style()
        self.style.configure("TButton", font=("TkDefaultFont", 16))
        self.style.map("TButton", background=[("active", "white"), ("pressed", "white")])

        self.light_mode = True

        # Call create_widgets function
        self.create_widgets()

    def create_widgets(self):
        menu = tk.Menu(self.master)
        self.master.config(menu=menu)
        file = tk.Menu(menu)
        file.add_command(label='Light mode', command=self.change_mode)
        file.add_separator()
        file.add_command(label='Math', command=self.open_math)
        file.add_command(label='Biology', command=self.open_biology)
        file.add_command(label='Physics', command=self.open_physics)
        file.add_command(label='Chemistry', command=self.open_chemistry)
        menu.add_cascade(label='Simulation', menu=file)

        # Create a new frame for the section below the menu bar
        self.button_frame = tk.Frame(self.master, bg=self.master['bg'])
        self.button_frame.pack(pady=10)

        # Add the four buttons to the frame
        self.mechanics_button = ttk.Button(self.button_frame, text="Mechanics", command=self.open_mechanics)
        self.mechanics_button.pack(side=tk.LEFT, padx=10)
        self.optics_button = ttk.Button(self.button_frame, text="Optics", command=self.open_optics)
        self.optics_button.pack(side=tk.LEFT, padx=10)
        self.waves_button = ttk.Button(self.button_frame, text="Waves", command=self.open_waves)
        self.waves_button.pack(side=tk.LEFT, padx=10)
        self.magnetism_button = ttk.Button(self.button_frame, text="Magnetism", command=self.open_magnetism)
        self.magnetism_button.pack(side=tk.LEFT, padx=10)

        # Add animations to the buttons
        self.animate_buttons()


def change_mode(self):
    if self.light_mode:
        self.master.configure(bg='#333')
        self.style.configure("TButton", font=("TkDefaultFont", 16), foreground="white", background="#333")
        self.style.map("TButton", background=[("active", "#333"), ("pressed", "#333")])
        self.light_mode = False
    else:
        self.master.configure(bg='white')
        self.style.configure("TButton", font=("TkDefaultFont", 16), foreground="black", background="white")
        self.style.map("TButton", background=[("active", "white"), ("pressed", "white")])
        self.light_mode = True


    # Create a new frame for the section below the menu bar
        self.button_frame = tk.Frame(self.master, bg=self.master['bg'])
        self.button_frame.pack(pady=10)

    # Add the four buttons to the frame
        self.mechanics_button = ttk.Button(self.button_frame, text="Mechanics", command=self.open_mechanics)
        self.mechanics_button.pack(side=tk.LEFT, padx=10)
        self.optics_button = ttk.Button(self.button_frame, text="Optics", command=self.open_optics)
        self.optics_button.pack(side=tk.LEFT, padx=10)
        self.waves_button = ttk.Button(self.button_frame, text="Waves", command=self.open_waves)
        self.waves_button.pack(side=tk.LEFT, padx=10)
        self.magnetism_button = ttk.Button(self.button_frame, text="Magnetism", command=self.open_magnetism)
        self.magnetism_button.pack(side=tk.LEFT, padx=10)

    # Add animations to the buttons
        self.animate_buttons()

def animate_buttons(self):
    # Define animation colors
    colors = cycle(['red', 'green', 'blue', 'yellow', 'cyan', 'magenta', 'white'])

    # Animate the button text color
    for button in (self.mechanics_button, self.optics_button, self.waves_button, self.magnetism_button):
        button.configure(foreground=next(colors))
    self.master.after(1000, self.animate_buttons)

    def open_mechanics(self):
        mechanics_window = tk.Toplevel(self.master)
        mechanics_window.geometry("400x300")
        mechanics_window.title("Mechanics")
        if self.light_mode:
            mechanics_window.configure(bg='white')
        else:
            mechanics_window.configure(bg='#333')
        mechanics_label = tk.Label(mechanics_window, text="Mechanics is the branch of physics that deals with the motion of objects and the forces that cause motion.", bg=mechanics_window['bg'], fg='black')
        mechanics_label.pack(pady=20)

def open_optics(self):
    optics_window = tk.Toplevel(self.master)
    optics_window.geometry("400x300")
    optics_window.title("Optics")
    if self.light_mode:
        optics_window.configure(bg='white')
    else:
        optics_window.configure(bg='#333')
    optics_label = tk.Label(optics_window, text="Optics is the branch of physics that studies the behavior and properties of light, including its interactions with matter and the construction of instruments that use or detect it.", bg=optics_window['bg'], fg='black')
    optics_label.pack(pady=20)

def open_waves(self):
    waves_window = tk.Toplevel(self.master)
    waves_window.geometry("400x300")
    waves_window.title("Waves")
    if self.light_mode:
        waves_window.configure(bg='white')
    else:
        waves_window.configure(bg='#333')
    waves_label =     tk.Label(waves_window, text="Waves is the branch of physics that deals with the study of waves, including their propagation, dispersion, and interference.", bg=waves_window['bg'], fg='black')
    waves_label.pack(pady=20)

def open_magnetism(self):
    magnetism_window = tk.Toplevel(self.master)
    magnetism_window.geometry("400x300")
    magnetism_window.title("Magnetism")
    if self.light_mode:
        magnetism_window.configure(bg='white')
    else:
        magnetism_window.configure(bg='#333')
    magnetism_label = tk.Label(magnetism_window, text="Magnetism is the branch of physics that deals with the study of magnetic fields and the forces they exert on moving electric charges and other magnetic materials.", bg=magnetism_window['bg'], fg='black')
    magnetism_label.pack(pady=20)

def change_mode(self):
    if self.light_mode:
        self.master.configure(bg='#333')
        self.style.configure("TButton", font=("TkDefaultFont", 16), foreground="white", background="#333")
        self.style.map("TButton", background=[("active", "#333"), ("pressed", "#333")])
        self.light_mode = False
    else:
        self.master.configure(bg='white')
        self.style.configure("TButton", font=("TkDefaultFont", 16), foreground="black", background="white")
    self.style.map("TButton", background=[("active", "white"), ("pressed", "white")])
    self.light_mode = True


def open_math(self):
        math_window = tk.Toplevel(self.master)
        math_window.geometry("400x300")
        math_window.title("Math")
        if self.light_mode:
            math_window.configure(bg='white')
        else:
            math_window.configure(bg='#333')
        math_label = tk.Label(math_window, text="Math is the study of topics such as quantity (numbers), structure, space, and change.", bg=math_window['bg'], fg='black')
        math_label.pack(pady=20)

def open_biology(self):
        biology_window = tk.Toplevel(self.master)
        biology_window.geometry("400x300")
        biology_window.title("Biology")
        if self.light_mode:
            biology_window.configure(bg='white')
        else:
            biology_window.configure(bg='#333')
        biology_label = tk.Label(biology_window, text="Biology is the natural science concerned with the study of life and living organisms, including their physical structure, chemical processes, molecular interactions, physiological mechanisms, development, and evolution.", bg=biology_window['bg'], fg='black')
        biology_label.pack(pady=20)

def open_physics(self):
        physics_window = tk.Toplevel(self.master)
        physics_window.geometry("400x300")
        physics_window.title("Physics")
        if self.light_mode:
            physics_window.configure(bg='white')
        else:
            physics_window.configure(bg='#333')
        physics_label = tk.Label(physics_window, text="Physics is the natural science that studies matter, its motion and behavior through space and time, and the related entities of energy and force.", bg=physics_window['bg'], fg='black')
        physics_label.pack(pady=20)

def open_chemistry(self):
        chemistry_window = tk.Toplevel(self.master)
        chemistry_window.geometry("400x300")
        chemistry_window.title("Chemistry")
        if self.light_mode:
            chemistry_window.configure(bg='white')
        else:
            chemistry_window.configure(bg='#333')
        chemistry_label = tk.Label(chemistry_window, text="Chemistry is the scientific discipline involved with elements and compounds composed of atoms, molecules, and ions: their composition, structure, properties, behavior, and the changes they undergo during a reaction with other substances.", bg=chemistry_window['bg'], fg='black')
        chemistry_label.pack(pady=20)

if __name__ == "__main__":
    root = tk.Tk()
    app = MainWindow(root)
    root.mainloop()
please i dont understand why this code isnt work and how to solve it. its show me

Exception has occurred: AttributeError
'MainWindow' object has no attribute 'change_mode'
File "C:\Users\Strix\Downloads\import tkinter as tk.py", line 29, in create_widgets
file.add_command(label='Light mode', command=self.change_mode)
File "C:\Users\Strix\Downloads\import tkinter as tk.py", line 23, in __init__
self.create_widgets()
File "C:\Users\Strix\Downloads\import tkinter as tk.py", line 197, in <module>
app = MainWindow(root)
AttributeError: 'MainWindow' object has no attribute 'change_mode'

please guys help me Smile
Reply
#2
The indentation was wrong. And animate_buttons() is wrong. I commented Line 52 #self.animate_buttons()

This is how the window opens.

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
 
# Import necessary modules for animations
import time
from itertools import cycle
 
class MainWindow:
    def __init__(self, master):
        self.master = master
        self.master.geometry("800x600")
        self.master.title("Physics Simulation")
        self.master.configure(bg='white')
 
        self.style = ttk.Style()
        self.style.configure("TButton", font=("TkDefaultFont", 16))
        self.style.map("TButton", background=[("active", "white"), ("pressed", "white")])
 
        self.light_mode = True
 
        # Call create_widgets function
        self.create_widgets()
 
    def create_widgets(self):
        menu = tk.Menu(self.master)
        self.master.config(menu=menu)
        file = tk.Menu(menu)
        file.add_command(label='Light mode', command=self.change_mode)
        file.add_separator()
        file.add_command(label='Math', command=self.open_math)
        file.add_command(label='Biology', command=self.open_biology)
        file.add_command(label='Physics', command=self.open_physics)
        file.add_command(label='Chemistry', command=self.open_chemistry)
        menu.add_cascade(label='Simulation', menu=file)
 
        # Create a new frame for the section below the menu bar
        self.button_frame = tk.Frame(self.master, bg=self.master['bg'])
        self.button_frame.pack(pady=10)
 
        # Add the four buttons to the frame
        self.mechanics_button = ttk.Button(self.button_frame, text="Mechanics", command=self.open_mechanics)
        self.mechanics_button.pack(side=tk.LEFT, padx=10)
        self.optics_button = ttk.Button(self.button_frame, text="Optics", command=self.open_optics)
        self.optics_button.pack(side=tk.LEFT, padx=10)
        self.waves_button = ttk.Button(self.button_frame, text="Waves", command=self.open_waves)
        self.waves_button.pack(side=tk.LEFT, padx=10)
        self.magnetism_button = ttk.Button(self.button_frame, text="Magnetism", command=self.open_magnetism)
        self.magnetism_button.pack(side=tk.LEFT, padx=10)
 
        # Add animations to the buttons
        #self.animate_buttons()
 
 
    def change_mode(self):
        if self.light_mode:
            self.master.configure(bg='#333')
            self.style.configure("TButton", font=("TkDefaultFont", 16), fg="white", bg="#333")
            self.style.map("TButton", background=[("active", "#333"), ("pressed", "#333")])
            self.light_mode = False
        else:
            self.master.configure(bg='white')
            self.style.configure("TButton", font=("TkDefaultFont", 16), fg="black", bg="white")
            self.style.map("TButton", background=[("active", "white"), ("pressed", "white")])
            self.light_mode = True
     
     
        # Create a new frame for the section below the menu bar
            self.button_frame = tk.Frame(self.master, bg=self.master['bg'])
            self.button_frame.pack(pady=10)
     
        # Add the four buttons to the frame
            self.mechanics_button = ttk.Button(self.button_frame, text="Mechanics", command=self.open_mechanics)
            self.mechanics_button.pack(side=tk.LEFT, padx=10)
            self.optics_button = ttk.Button(self.button_frame, text="Optics", command=self.open_optics)
            self.optics_button.pack(side=tk.LEFT, padx=10)
            self.waves_button = ttk.Button(self.button_frame, text="Waves", command=self.open_waves)
            self.waves_button.pack(side=tk.LEFT, padx=10)
            self.magnetism_button = ttk.Button(self.button_frame, text="Magnetism", command=self.open_magnetism)
            self.magnetism_button.pack(side=tk.LEFT, padx=10)
     
        # Add animations to the buttons
            self.animate_buttons()
     
    def animate_buttons(self):
        # Define animation colors
        colors = cycle(['red', 'green', 'blue', 'yellow', 'cyan', 'magenta', 'white'])
        # Animate the button text color
        for button in ([self.mechanics_button, self.optics_button, self.waves_button, self.magnetism_button]):
            button.configure(fg=next(colors))
            
        self.master.after(1000, self.animate_buttons)
     
    def open_mechanics(self):
        mechanics_window = tk.Toplevel(self.master)
        mechanics_window.geometry("400x300")
        mechanics_window.title("Mechanics")
        if self.light_mode:
            mechanics_window.configure(bg='white')
        else:
            mechanics_window.configure(bg='#333')
        mechanics_label = tk.Label(mechanics_window, text="Mechanics is the branch of physics that deals with the motion of objects and the forces that cause motion.", bg=mechanics_window['bg'], fg='black')
        mechanics_label.pack(pady=20)
     
    def open_optics(self):
        optics_window = tk.Toplevel(self.master)
        optics_window.geometry("400x300")
        optics_window.title("Optics")
        if self.light_mode:
            optics_window.configure(bg='white')
        else:
            optics_window.configure(bg='#333')
        optics_label = tk.Label(optics_window, text="Optics is the branch of physics that studies the behavior and properties of light, including its interactions with matter and the construction of instruments that use or detect it.", bg=optics_window['bg'], fg='black')
        optics_label.pack(pady=20)
     
    def open_waves(self):
        waves_window = tk.Toplevel(self.master)
        waves_window.geometry("400x300")
        waves_window.title("Waves")
        if self.light_mode:
            waves_window.configure(bg='white')
        else:
            waves_window.configure(bg='#333')
        waves_label =     tk.Label(waves_window, text="Waves is the branch of physics that deals with the study of waves, including their propagation, dispersion, and interference.", bg=waves_window['bg'], fg='black')
        waves_label.pack(pady=20)
     
    def open_magnetism(self):
        magnetism_window = tk.Toplevel(self.master)
        magnetism_window.geometry("400x300")
        magnetism_window.title("Magnetism")
        if self.light_mode:
            magnetism_window.configure(bg='white')
        else:
            magnetism_window.configure(bg='#333')
        magnetism_label = tk.Label(magnetism_window, text="Magnetism is the branch of physics that deals with the study of magnetic fields and the forces they exert on moving electric charges and other magnetic materials.", bg=magnetism_window['bg'], fg='black')
        magnetism_label.pack(pady=20)
     
    def change_mode(self):
        if self.light_mode:
            self.master.configure(bg='#333')
            self.style.configure("TButton", font=("TkDefaultFont", 16), fg="white", bg="#333")
            self.style.map("TButton", background=[("active", "#333"), ("pressed", "#333")])
            self.light_mode = False
        else:
            self.master.configure(bg='white')
            self.style.configure("TButton", font=("TkDefaultFont", 16), fg="black", bg="white")
        self.style.map("TButton", background=[("active", "white"), ("pressed", "white")])
        self.light_mode = True
     
     
    def open_math(self):
            math_window = tk.Toplevel(self.master)
            math_window.geometry("400x300")
            math_window.title("Math")
            if self.light_mode:
                math_window.configure(bg='white')
            else:
                math_window.configure(bg='#333')
            math_label = tk.Label(math_window, text="Math is the study of topics such as quantity (numbers), structure, space, and change.", bg=math_window['bg'], fg='black')
            math_label.pack(pady=20)
     
    def open_biology(self):
            biology_window = tk.Toplevel(self.master)
            biology_window.geometry("400x300")
            biology_window.title("Biology")
            if self.light_mode:
                biology_window.configure(bg='white')
            else:
                biology_window.configure(bg='#333')
            biology_label = tk.Label(biology_window, text="Biology is the natural science concerned with the study of life and living organisms, including their physical structure, chemical processes, molecular interactions, physiological mechanisms, development, and evolution.", bg=biology_window['bg'], fg='black')
            biology_label.pack(pady=20)
     
    def open_physics(self):
            physics_window = tk.Toplevel(self.master)
            physics_window.geometry("400x300")
            physics_window.title("Physics")
            if self.light_mode:
                physics_window.configure(bg='white')
            else:
                physics_window.configure(bg='#333')
            physics_label = tk.Label(physics_window, text="Physics is the natural science that studies matter, its motion and behavior through space and time, and the related entities of energy and force.", bg=physics_window['bg'], fg='black')
            physics_label.pack(pady=20)
     
    def open_chemistry(self):
            chemistry_window = tk.Toplevel(self.master)
            chemistry_window.geometry("400x300")
            chemistry_window.title("Chemistry")
            if self.light_mode:
                chemistry_window.configure(bg='white')
            else:
                chemistry_window.configure(bg='#333')
            chemistry_label = tk.Label(chemistry_window, text="Chemistry is the scientific discipline involved with elements and compounds composed of atoms, molecules, and ions: their composition, structure, properties, behavior, and the changes they undergo during a reaction with other substances.", bg=chemistry_window['bg'], fg='black')
            chemistry_label.pack(pady=20)
     
if __name__ == "__main__":
    root = tk.Tk()
    app = MainWindow(root)
    root.mainloop()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020