如何在 Python 中更改我的 tkinter 应用程序的主题?
How to change the theme of my tkinter application in Python?
我正在尝试更改 Tkinter
主题,但是当我将 s.theme_use('classic')
更改为 s.theme_use('calm')
或 s.theme_use('winnative')
时,没有任何变化。
这是我的代码:
from tkinter import *
import tkinter.ttk as ttk
window = Tk()
window.title("Running Python Script") # Create window
window.geometry('550x300') # geo of the window
s=ttk.Style()
list_themes = s.theme_names()
current_theme = s.theme_use()
s.theme_use('classic')
print(list_themes)
def run():
if dd_owner.get() == "Spain":
print("spain")
# These are the option menus
dd_owner = StringVar(window)
dd_owner.set(owner[0]) # the first value
w = OptionMenu(window, dd_owner, *owner)
w.grid(row=0, column=1)
#The run button
run_button = Button(window, text="Run application {}".format(dd_owner.get()), bg="blue", fg="white",command=run)
run_button.grid(column=0, row=2)
# These are the titles
l1 = Label(window, text='Select Owner', width=15)
l1.grid(row=0, column=0)
mainloop()
下面是根据您的代码使用 ttk
小部件的修改示例:
from tkinter import *
import tkinter.ttk as ttk
import random
window = Tk()
window.title("Running Python Script") # Create window
window.geometry('550x300') # geo of the window
s=ttk.Style()
s.configure("TButton", foreground="red", background="blue")
list_themes = s.theme_names()
current_theme = s.theme_use()
#s.theme_use('classic')
print(list_themes)
def run():
if dd_owner.get() == "Spain":
print("spain")
# choose a theme randomly
theme = random.choice(list_themes)
print("theme:", theme)
s.theme_use(theme)
# These are the option menus
owner = ("Spain", "France", "Germany")
dd_owner = StringVar(window)
dd_owner.set(owner[0]) # the first value
#w = OptionMenu(window, dd_owner, *owner)
# use ttk.Combobox instead of OptionMenu
w = ttk.Combobox(window, textvariable=dd_owner, values=owner)
w.grid(row=0, column=1)
#The run button
#run_button = Button(window, text="Run application {}".format(dd_owner.get()), bg="blue", fg="white",command=run)
# use ttk.Button
run_button = ttk.Button(window, text="Run application {}".format(dd_owner.get()), command=run)
run_button.grid(column=0, row=2)
# These are the titles
l1 = ttk.Label(window, text='Select Owner', width=15)
l1.grid(row=0, column=0)
mainloop()
当你点击按钮时,它会随机改变主题。
我正在尝试更改 Tkinter
主题,但是当我将 s.theme_use('classic')
更改为 s.theme_use('calm')
或 s.theme_use('winnative')
时,没有任何变化。
这是我的代码:
from tkinter import *
import tkinter.ttk as ttk
window = Tk()
window.title("Running Python Script") # Create window
window.geometry('550x300') # geo of the window
s=ttk.Style()
list_themes = s.theme_names()
current_theme = s.theme_use()
s.theme_use('classic')
print(list_themes)
def run():
if dd_owner.get() == "Spain":
print("spain")
# These are the option menus
dd_owner = StringVar(window)
dd_owner.set(owner[0]) # the first value
w = OptionMenu(window, dd_owner, *owner)
w.grid(row=0, column=1)
#The run button
run_button = Button(window, text="Run application {}".format(dd_owner.get()), bg="blue", fg="white",command=run)
run_button.grid(column=0, row=2)
# These are the titles
l1 = Label(window, text='Select Owner', width=15)
l1.grid(row=0, column=0)
mainloop()
下面是根据您的代码使用 ttk
小部件的修改示例:
from tkinter import *
import tkinter.ttk as ttk
import random
window = Tk()
window.title("Running Python Script") # Create window
window.geometry('550x300') # geo of the window
s=ttk.Style()
s.configure("TButton", foreground="red", background="blue")
list_themes = s.theme_names()
current_theme = s.theme_use()
#s.theme_use('classic')
print(list_themes)
def run():
if dd_owner.get() == "Spain":
print("spain")
# choose a theme randomly
theme = random.choice(list_themes)
print("theme:", theme)
s.theme_use(theme)
# These are the option menus
owner = ("Spain", "France", "Germany")
dd_owner = StringVar(window)
dd_owner.set(owner[0]) # the first value
#w = OptionMenu(window, dd_owner, *owner)
# use ttk.Combobox instead of OptionMenu
w = ttk.Combobox(window, textvariable=dd_owner, values=owner)
w.grid(row=0, column=1)
#The run button
#run_button = Button(window, text="Run application {}".format(dd_owner.get()), bg="blue", fg="white",command=run)
# use ttk.Button
run_button = ttk.Button(window, text="Run application {}".format(dd_owner.get()), command=run)
run_button.grid(column=0, row=2)
# These are the titles
l1 = ttk.Label(window, text='Select Owner', width=15)
l1.grid(row=0, column=0)
mainloop()
当你点击按钮时,它会随机改变主题。