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 |1 Answer
Reset to default 0The 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)
...
本文标签:
版权声明:本文标题:python - opening a stylized Tkinter GUI from a Tkinter button click event doesn't apply the style - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745515390a2661507.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Toplevel
instead ofTk
forSecondApp
. – acw1668 Commented Nov 28, 2024 at 10:50self.root = tk.Tk()
toself.root = tk.Toplevel()
insideopen_app()
. – acw1668 Commented Nov 28, 2024 at 10:55