首页 > 解决方案 > 为什么连接样式时会出现两个窗口?

问题描述

在此处输入图像描述我有这样的代码:

from tkinter import Tk
import tkinter.ttk as ttk

"""Styles"""
style = ttk.Style()
style.configure('OrangeButton.TButton', foreground='white', background='#ff9203')
style.map('OrangeButton.TButton',
          foreground=[('pressed', 'white'), ('active', 'white')],
          background=[('pressed', '!disabled', '#adadad'), ('active', '#de8e26')])

root = Tk()

button = ttk.Button(root, text="Ok", width=20, style='OrangeButton.TButton')
button.pack(padx=50, pady=50)

root.mainloop()

我是新来的。我在互联网上搜索了解决方案,但找不到。他们到处写关于widthdraw()的文章,但这无济于事。始终出现两个窗口,并且自定义样式未应用于按钮。我究竟做错了什么?如何在 Google 上搜索此问题?请告诉我。谢谢。

标签: python-3.xtkinterstylesttk

解决方案


您只需要ttk.Style()root窗口内定义。

from tkinter import Tk
import tkinter.ttk as ttk

root = Tk()

"""Styles"""
style = ttk.Style()

# add the these_use option and use the 'clam' theme
style.theme_use('clam')

style.configure('OrangeButton.TButton', foreground='white', background='#ff9203')
style.map('OrangeButton.TButton',
          foreground=[('pressed', 'white'), ('active', 'white')],
          background=[('pressed', '!disabled', '#adadad'), ('active', '#de8e26')])

button = ttk.Button(root, text="Ok", width=20, style='OrangeButton.TButton')
button.pack(padx=50, pady=50)

root.mainloop()

希望这能解决问题。


推荐阅读