gpt4 book ai didi

tkinter - ttk 创建和使用自定义主题

转载 作者:行者123 更新时间:2023-12-05 01:02:10 33 4
gpt4 key购买 nike

我正在尝试在我的程序中加入可自定义的主题选择。我一直在引用这个指南:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-themes.html ,但我迷路了。

这是我一直用来解决这个问题的示例代码。我正确地创建了一个新主题“阴影”,但下一部分我正在努力。让我们保持简单,在“阴影”主题中说,我想将框架背景更改为黑色,将按钮背景更改为海军蓝,将按钮前景更改为白色。

我该怎么做?

from tkinter import *
from tkinter import ttk

class Main:
def __init__(self, master):
self.master = master
self.main_button = ttk.Button(self.master, text = 'New', command = self.new_window)
self.main_button.grid()

def new_window(self):
pop_up = Top(self.master)

class Top:
def __init__(self, master):
pop_up = self.pop_up = Toplevel(master)
self.pop_up_frame = ttk.Frame(pop_up, height = 100, width = 100)
self.pop_up_frame.grid(sticky = E+W+S+N)
self.s = ttk.Style()
self.s.theme_create('shadow', parent = 'default')

print(self.s.theme_names())

self.c1_button = ttk.Button(pop_up, text = 'Default', command = self.get_default)
self.c2_button = ttk.Button(pop_up, text = 'Shadow', command = self.get_shadow)

self.c1_button.grid()
self.c2_button.grid()

def get_default(self):
self.s.theme_use('default')

def get_shadow(self):
self.s.theme_use('shadow')


root = Tk()

app = Main(root)

root.mainloop()

一如既往,谢谢!

最佳答案

这太难了。

这是其他任何人试图解决这个问题的解决方案:

from tkinter import *
from tkinter import ttk

class Main:
def __init__(self, master):
self.master = master
self.main_button = ttk.Button(self.master, text = 'New', command = self.new_window)
self.main_button.grid()

def new_window(self):
pop_up = Top(self.master)

class Top:
def __init__(self, master):
pop_up = self.pop_up = Toplevel(master)
self.pop_up_frame = ttk.Frame(pop_up, height = 100, width = 100)
self.pop_up_frame.grid(sticky = E+W+S+N)
self.s = ttk.Style()
self.s.theme_create('shadow', parent = 'default')

print(self.s.theme_names())

self.c1_button = ttk.Button(pop_up, text = 'Default', command = self.get_default)
self.c2_button = ttk.Button(pop_up, text = 'Vista', command = self.get_shadow)

self.c1_button.grid()
self.c2_button.grid()

def get_default(self):
self.s.theme_use('default')

def get_shadow(self):
self.s.theme_use('vista')
self.s.configure('TButton', foreground = 'white', background = 'blue')
self.s.configure('TFrame', background = 'black')



root = Tk()

app = Main(root)

root.mainloop()

关于tkinter - ttk 创建和使用自定义主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35439598/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com