admin管理员组

文章数量:1429376

I have two files in a single folder: MainApp.py and SecondApp.py.

Both have ttk.Style() applied to specific controls. And when you launch them individually, the styles are implemented accordingly.


MainApp:

Code:

import tkinter as tk
from tkinter import ttk, PhotoImage
from SecondApp import SecondApp

class MainApp:
    def __init__(self, app):
        self.app = app
        self.app.title("MainApp")
        self.app.geometry("200x200")
        self.app.config(bg="#FFFFFF")

    def create_styles(self):
        self.style = ttk.Style()
        self.style.theme_use('clam')

        self.style.configure('HELP_BUTTON.TButton',
                        padding=10,
                        relief='flat',
                        background="#625bcb",
                        foreground='white')
        self.style.map('HELP_BUTTON.TButton',
                background=[('active', '#352cbc'), ('disabled', '#acacac')],
                foreground=[('active', 'white'), ('disabled', 'gray')])
        
    def render_widget(self):
        self.btn_help_img = PhotoImage(file=fr"D:\question_mark_25px.png", master=self.app)
        self.btn_help = ttk.Button(self.app, padding=(0, -1), image=self.btn_help_img, style="HELP_BUTTON.TButton")
        self.btn_help.config(cursor="hand2")
        self.btn_help.place(x=10, y=10, width=28, height=28)
        self.btn_help.bind("<Button-1>", self.open_app)


    def open_app(self, event):
        self.root = tk.Tk()
        self.sa = SecondApp(self.root)
        self.sa.create_styles()
        self.sa.render_widget()



if __name__ == "__main__":
    root = tk.Tk()

    rrt = MainApp(root)

    rrt.create_styles()
    rrt.render_widget()

    rrt.app.mainloop()

SecondApp:

Code:

from tkinter import ttk

class SecondApp:
    def __init__(self, app):
        self.app = app
        self.app.title("SecondApp")
        self.app.geometry("200x200")
        self.app.config(bg="#FFFFFF")
        
        self.HEADER_FONT = ("Istok Web", 9, "bold")

    def create_styles(self):
        self.style = ttk.Style()
        self.style.theme_use('clam')

        # Debugging output
        print("Creating styles for SecondApp")

        self.style.configure('FLAT_BUTTON.TButton',
                            padding=8,
                            font=self.HEADER_FONT,
                            relief='flat',
                            background='#DDDDDD',
                            foreground='#302C2c')
        self.style.map('FLAT_BUTTON.TButton',
                    background=[('active', '#cac6c6'), ('disabled', '#acacac')],
                    foreground=[('active', 'black'), ('disabled', 'gray')])
        
    def render_widget(self):
        # Debugging output
        print("Rendering widget in SecondApp")

        self.btn_startProcessing = ttk.Button(self.app, text="START PROCESSING", style="FLAT_BUTTON.TButton")
        self.btn_startProcessing.place(x=10, y=10, width=150, height=40)

However, when I link the launching of SecondApp from within a button click in MainApp, the style for SecondApp doesn't load. The styles are encapsulated inside their respective classes, and I have tried numerous modifications to my code but to no success.

I am wondering if anybody have encountered the same issue and was able to fix?

I have two files in a single folder: MainApp.py and SecondApp.py.

Both have ttk.Style() applied to specific controls. And when you launch them individually, the styles are implemented accordingly.


MainApp:

Code:

import tkinter as tk
from tkinter import ttk, PhotoImage
from SecondApp import SecondApp

class MainApp:
    def __init__(self, app):
        self.app = app
        self.app.title("MainApp")
        self.app.geometry("200x200")
        self.app.config(bg="#FFFFFF")

    def create_styles(self):
        self.style = ttk.Style()
        self.style.theme_use('clam')

        self.style.configure('HELP_BUTTON.TButton',
                        padding=10,
                        relief='flat',
                        background="#625bcb",
                        foreground='white')
        self.style.map('HELP_BUTTON.TButton',
                background=[('active', '#352cbc'), ('disabled', '#acacac')],
                foreground=[('active', 'white'), ('disabled', 'gray')])
        
    def render_widget(self):
        self.btn_help_img = PhotoImage(file=fr"D:\question_mark_25px.png", master=self.app)
        self.btn_help = ttk.Button(self.app, padding=(0, -1), image=self.btn_help_img, style="HELP_BUTTON.TButton")
        self.btn_help.config(cursor="hand2")
        self.btn_help.place(x=10, y=10, width=28, height=28)
        self.btn_help.bind("<Button-1>", self.open_app)


    def open_app(self, event):
        self.root = tk.Tk()
        self.sa = SecondApp(self.root)
        self.sa.create_styles()
        self.sa.render_widget()



if __name__ == "__main__":
    root = tk.Tk()

    rrt = MainApp(root)

    rrt.create_styles()
    rrt.render_widget()

    rrt.app.mainloop()

SecondApp:

Code:

from tkinter import ttk

class SecondApp:
    def __init__(self, app):
        self.app = app
        self.app.title("SecondApp")
        self.app.geometry("200x200")
        self.app.config(bg="#FFFFFF")
        
        self.HEADER_FONT = ("Istok Web", 9, "bold")

    def create_styles(self):
        self.style = ttk.Style()
        self.style.theme_use('clam')

        # Debugging output
        print("Creating styles for SecondApp")

        self.style.configure('FLAT_BUTTON.TButton',
                            padding=8,
                            font=self.HEADER_FONT,
                            relief='flat',
                            background='#DDDDDD',
                            foreground='#302C2c')
        self.style.map('FLAT_BUTTON.TButton',
                    background=[('active', '#cac6c6'), ('disabled', '#acacac')],
                    foreground=[('active', 'black'), ('disabled', 'gray')])
        
    def render_widget(self):
        # Debugging output
        print("Rendering widget in SecondApp")

        self.btn_startProcessing = ttk.Button(self.app, text="START PROCESSING", style="FLAT_BUTTON.TButton")
        self.btn_startProcessing.place(x=10, y=10, width=150, height=40)

However, when I link the launching of SecondApp from within a button click in MainApp, the style for SecondApp doesn't load. The styles are encapsulated inside their respective classes, and I have tried numerous modifications to my code but to no success.

I am wondering if anybody have encountered the same issue and was able to fix?

Share Improve this question asked Nov 28, 2024 at 10:24 NiiNii 57810 silver badges31 bronze badges 3
  • Use Toplevel instead of Tk for SecondApp. – acw1668 Commented Nov 28, 2024 at 10:50
  • its actually an existing app. i need to link it to the new app. it should launch in a click of a button. – Nii Commented Nov 28, 2024 at 10:51
  • What I mean is to change self.root = tk.Tk() to self.root = tk.Toplevel() inside open_app(). – acw1668 Commented Nov 28, 2024 at 10:55
Add a comment  | 

1 Answer 1

Reset to default 0

The issue is caused by multiple instance of tk.Tk. To fix this, either specify the master option when creating the instance of ttk.Style:

SecondApp

class SecondApp:
    ...

    def create_styles(self):
        self.style = ttk.Style(self.app)  # specify master
        ...

Or use tk.Toplevel instead of tk.Tk for the master of SecondApp:

MainApp

class MainApp:
    ...

    def open_app(self, event):
        self.root = tk.Toplevel()   # use Toplevel instead of Tk
        self.sa = SecondApp(self.root)
        ...

本文标签: