Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 为什么在连接样式时会出现两个窗口?_Python 3.x_Tkinter_Styles_Ttk - Fatal编程技术网

Python 3.x 为什么在连接样式时会出现两个窗口?

Python 3.x 为什么在连接样式时会出现两个窗口?,python-3.x,tkinter,styles,ttk,Python 3.x,Tkinter,Styles,Ttk,我有这样一个代码: 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'

我有这样一个代码:

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(),但这没有帮助。始终会出现两个窗口,自定义样式不会应用于按钮。我做错了什么?我如何在谷歌上搜索这个问题?请告诉我。谢谢。

您只需要在
root
窗口中定义
ttk.Style()

从tkinter导入Tk
将tkinter.ttk导入为ttk
root=Tk()
“样式”
style=ttk.style()
#添加this_use选项并使用“clam”主题
风格。主题\使用(“蛤蜊”)
style.configure('OrangeButton.TButton',前台='white',后台='#ff9203')
style.map('OrangeButton.TButton',
前景=[(“按下”,“白色”),(“活动”,“白色”)],
背景=[('pressed'、'disabled'、'adadad')、('active'、'de8e26'))
按钮=ttk.button(根,text=“Ok”,宽度=20,style='OrangeButton.TButton')
按钮组(padx=50,pady=50)
root.mainloop()

希望这能解决问题。

谢谢你的回答。这有助于移除第二个窗口。但由于某些原因,风格并不完全有效。我阅读了ttk文档,也做了同样的事情,但它不起作用。我表示按钮是橙色的,但它是灰色的。如何修复它?抱歉这个愚蠢的问题。嘿@rdbrg,我建议你使用
tk按钮
。与
ttk
窗口相比,它更易于自定义。因为我的解决方案解决了您的问题,即在连接样式时为什么会出现两个窗口?请接受答案,因为它将帮助社区认识到正确的解决方案。干杯嘿@rdbrg,我更新了代码添加了
样式。当然,我使用('clam')
来制作按钮
橙色。但我正在学习ttk。你的建议“Tk按钮会更容易”是可以理解的,但对我的学习没有帮助。谢谢你的回复。