如何在Python中更改tkinter应用程序的主题?

2024-05-23 13:48:49 发布

您现在位置: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()

Tags: therunimportusetkintercolumnbuttonwindow
1条回答
网友
1楼 · 发布于 2024-05-23 13:48:49

下面是一个基于代码使用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()

当你点击按钮时,它会随机改变主题

相关问题 更多 >