
sv_ttk 库为 tkinter 提供了现代化的 sun valley 主题,其便捷性在于只需一行代码 sv_ttk.use_dark_theme() 或 sv_ttk.use_light_theme() 即可为应用程序应用主题。然而,在以下场景中,这种便捷性可能导致问题:
问题通常表现为 _tkinter.TclError: can't invoke "winfo" command: application has been destroyed。这表明 sv_ttk 库在内部可能尝试访问一个已经被销毁的 Tkinter 实例(通常是第一个 Tk() 根窗口),或者其主题管理机制与多实例环境不兼容。sv_ttk 库的设计可能更侧重于单个主窗口的应用,其全局状态管理在复杂的多窗口场景下会暴露出局限性。
考虑以下简化代码示例,展示了导致错误的基本模式:
import tkinter as t
import tkinter.ttk as ttk
import sv_ttk
def create_main_window():
root = t.Tk()
root.title("主窗口")
ttk.Label(root, text="这是主窗口").pack(pady=20)
sv_ttk.use_dark_theme() # 首次设置主题
root.destroy() # 销毁主窗口
root.mainloop()
def create_popup_window():
popup = t.Tk() # 创建新的根窗口
popup.title("弹出窗口")
ttk.Label(popup, text="这是弹出窗口").pack(pady=20)
sv_ttk.use_dark_theme() # 再次设置主题,可能导致错误
popup.mainloop()
# 模拟场景:主窗口销毁后创建弹出窗口
create_main_window()
create_popup_window() # 在这里可能会抛出 TclError解决此问题的最佳方法是绕过 sv_ttk 库的全局管理,转而采用 Tkinter 原生的 Tcl/Tk 命令来加载和应用主题。sv_ttk 实际上是基于 Tcl/Tk 主题引擎的 Python 封装。通过直接使用 Tcl 脚本,我们可以确保每个 Tkinter 实例都拥有独立的主题上下文。
sv_ttk 主题的样式定义存储在 .tcl 文件中。你需要将这些文件(例如 sun-valley.tcl 和 sun-valley-dark.tcl 或 sun-valley-light.tcl)放置在你的项目目录中,或者一个可访问的路径下。你可以从 sv_ttk 的 GitHub 仓库或其依赖的 SunValleyttktheme 项目中获取这些文件。一个方便的来源是 https://www.php.cn/link/c9db91a33f7c9b95eeb17aa5d3cdef5c。
假设你将 sun-valley.tcl 和 sun-valley-dark.tcl 放在了项目根目录下的 images/THEME/ 文件夹中。
对于每个 tkinter.Tk() 或 tkinter.Toplevel() 实例,你需要执行以下操作:
关键点: 每次创建新的 Tk 或 Toplevel 实例时,都需要重新执行这两个 Tcl 命令,以确保该实例能够正确加载和应用主题。
让我们根据原始问题中的代码,将其主题设置部分进行修改:
import tkinter as t
import tkinter.ttk as ttk
from tkinter.messagebox import showerror
import os
import json
import webbrowser
# from PIL import Image, ImageTk # 假设已安装 Pillow
# import ntkutils # 假设存在此工具,用于设置暗色标题栏
# import pygame as p # 假设存在此库,用于颜色定义
# 定义全局变量,用于演示
SKIN = "Default"
THEME = "Default"
COLORS = [t.Color(240, 217, 181), t.Color(181, 136, 99)] # 示例颜色
FRAMES_PER_SQUARE = 5
PROMOTION_PIECE = "Queen"
# 主题文件路径 (请根据实际情况调整)
THEME_TCL_PATH = './images/THEME/sun-valley.tcl' # 假设 sun-valley.tcl 包含所有主题定义
def apply_sun_valley_theme(instance, theme_name='dark'):
"""
为给定的 Tkinter 实例加载 Sun Valley 主题并应用指定样式。
"""
if not os.path.exists(THEME_TCL_PATH):
print(f"错误: 主题文件未找到于 {THEME_TCL_PATH}")
return
try:
instance.tk.call('source', THEME_TCL_PATH)
instance.tk.call('set_theme', theme_name)
except t.TclError as e:
print(f"应用主题时发生 TclError: {e}")
# 如果主题文件损坏或路径错误,可能会出现此错误
except Exception as e:
print(f"应用主题时发生未知错误: {e}")
def choose_skin_theme():
"""
显示一个 GUI 窗口,允许用户选择棋盘的皮肤和主题。
"""
global SKIN, THEME, COLORS, FRAMES_PER_SQUARE
def load_chess_data(file_path):
if not os.path.isfile(file_path):
return None
with open(file_path, 'r') as file:
return json.load(file)
def show_last_moves():
file_path = ".moves_log.json"
chess_data = load_chess_data(file_path)
if chess_data:
show_chess_data(chess_data)
else:
showerror("ERROR", "No data to show or error loading data.")
def apply_selection():
global SKIN, THEME, COLORS, FRAMES_PER_SQUARE
SKIN = skin_combo.get()
selected_theme = theme_combo.get() # 获取用户选择的主题
THEME = selected_theme
# 根据选择更新颜色(示例逻辑)
if selected_theme == 'Default':
COLORS = ["#F0D9B5", "#B58863"] # 示例颜色
elif selected_theme == 'Dark':
COLORS = ["#969696", "#323232"] # 示例颜色
elif selected_theme == 'Green':
COLORS = ["#EEEDD2", "#769656"] # 示例颜色
FRAMES_PER_SQUARE = int(anim_combo.get()[0])
shutdown_ttk_repeat()
def shutdown_ttk_repeat():
# root.eval('::ttk::CancelRepeat') # 如果有 ttk::CancelRepeat 需要,请保留
root.destroy()
def open_github():
webbrowser.open("https://github.com/t0ry003/GoodChess")
def show_chess_data(chess_data):
top = t.Toplevel()
# ntkutils.dark_title_bar(top) # 假设 ntkutils 存在
top.title("Data Viewer")
top.iconbitmap("images/game/icon.ico")
top_window_width = 280
top_window_height = 250
top_screen_width = top.winfo_screenwidth()
top_screen_height = top.winfo_screenheight()
top_x_position = (top_screen_width - top_window_width) // 2
top_y_position = (top_screen_height - top_window_height) // 2
top.geometry(f"{top_window_width}x{top_window_height}+{top_x_position}+{top_y_position}")
# 为 Toplevel 窗口应用主题
apply_sun_valley_theme(top, 'dark') # 默认使用暗色主题
tree = ttk.Treeview(top, columns=('No', 'Player', 'Move'), show='headings', style='Treeview')
tree.heading('No', text='No', anchor='center')
tree.heading('Player', text='Player', anchor='center')
tree.heading('Move', text='Move', anchor='center')
scroll = ttk.Scrollbar(top, orient='vertical', command=tree.yview)
for move in chess_data:
tree.insert('', 'end', values=(move['number'], move['player'], move['move']))
tree.column('No', width=30)
tree.column('Player', width=100)
tree.column('Move', width=100)
tree.configure(yscrollcommand=scroll.set)
scroll.pack(side='right', fill='y')
tree.pack(side='left', fill='both', expand=True)
top.mainloop()
root = t.Tk()
# ntkutils.dark_title_bar(root) # 假设 ntkutils 存在
root.title("Good Chess | Settings")
root.iconbitmap("images/game/icon.ico")
window_width = 350
window_height = 625
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_position = (screen_width - window_width) // 2
y_position = (screen_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")
# 为主窗口应用主题
apply_sun_valley_theme(root, 'dark') # 默认使用暗色主题
# main_logo = ImageTk.PhotoImage(Image.open("./images/GAME/icon.ico").resize((150, 150)))
# play_icon = t.PhotoImage(file='./images/GAME/play-icon.png')
skin_label = ttk.Label(root, text="Choose Skin:")
skin_combo = ttk.Combobox(root, values=["Default", "Fantasy", "Minimalist"])
skin_combo.set(SKIN)
theme_label = ttk.Label(root, text="Choose Theme:")
theme_combo = ttk.Combobox(root, values=["Default", "Dark", "Green"])
theme_combo.set(THEME)
anim_label = ttk.Label(root, text="Choose Animation Speed:")
anim_combo = ttk.Combobox(root, width=1, values=["1 (FAST)", "2", "3", "4", "5", "6", "7", "8", "9 (SLOW)"])
anim_combo.set(FRAMES_PER_SQUARE)
# logo_label = ttk.Label(root, image=main_logo)
apply_button = ttk.Button(root, text="START", command=apply_selection) #, image=play_icon, compound=t.LEFT)
show_moves_button = ttk.Button(root, text="Show Last Moves", command=show_last_moves)
github_button = ttk.Button(root, text="\u2B50 GitHub", command=open_github)
# logo_label.pack(pady=10)
skin_label.pack(pady=10)
skin_combo.pack(pady=10)
theme_label.pack(pady=10)
theme_combo.pack(pady=10)
anim_label.pack(pady=10)
anim_combo.pack(pady=10)
apply_button.pack(pady=20)
show_moves_button.pack(pady=10)
github_button.pack(side=t.LEFT, padx=10, pady=10)
root.protocol("WM_DELETE_WINDOW", shutdown_ttk_repeat)
root.mainloop()
def askPawnPromotion():
"""
询问玩家将兵提升为什么棋子。
"""
global PROMOTION_PIECE
def apply_selection():
global PROMOTION_PIECE
PROMOTION_PIECE = promotion_combo.get()
popup.destroy()
# popup.quit() # 在 Toplevel 中通常不需要调用 quit()
popup = t.Tk() # 原始代码是 t.Tk(),如果是一个子窗口,通常会是 t.Toplevel()
# ntkutils.dark_title_bar(popup) # 假设 ntkutils 存在
popup.title("Good Chess | Pawn Promotion")
popup.iconbitmap("images/GAME/icon.ico")
window_width = 350
window_height = 200
screen_width = popup.winfo_screenwidth()
screen_height = popup.winfo_screenheight()
x_position = (screen_width - window_width) // 2
y_position = (screen_height - window_height) // 2
popup.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")
# 为弹出窗口应用主题
apply_sun_valley_theme(popup, 'dark') # 默认使用暗色主题
promotion_label = ttk.Label(popup, text="Choose a piece to promote the pawn to:")
promotion_combo = ttk.Combobox(popup, values=["Queen", "Rook", "Bishop", "Knight"])
promotion_combo.set("Queen")
apply_button = ttk.Button(popup, text="APPLY", command=apply_selection)
promotion_label.pack(pady=10)
promotion_combo.pack(pady=10)
apply_button.pack(pady=20)
popup.mainloop()
return PROMOTION_PIECE[0]
# 示例调用
if __name__ == "__main__":
# 请确保 'images/THEME/sun-valley.tcl' 路径正确
# 假设 images/GAME/icon.ico 等资源文件也存在
# 为了运行示例,可能需要注释掉 Image, ImageTk, ntkutils, pygame 的导入和使用
# 模拟主窗口和弹出窗口的交互
choose_skin_theme() # 主窗口被销毁
# 此时如果再次创建 Tk() 实例,并调用 sv_ttk.use_dark_theme(),就会出现问题
# 但通过 apply_sun_valley_theme 函数,每次都能独立设置主题
# askPawnPromotion() # 可以在需要时调用代码修改说明:
通过上述方法,你可以有效地管理 Tkinter 应用程序中多个窗口的 sv_ttk 主题,避免因窗口销毁或多实例冲突导致的主题错误,从而构建更健壮、用户体验更一致的 GUI 应用。
以上就是Tkinter sv_ttk 主题在多窗口应用中的正确使用与错误规避的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号