【问题标题】:Why do two windows appear when I connect styles?为什么连接样式时会出现两个窗口?
【发布时间】:2020-01-22 07:16:39
【问题描述】:

enter image description here我有这样的代码:

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.x tkinter styles ttk


    【解决方案1】:

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

    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()
    

    希望这能解决问题。

    【讨论】:

    • 感谢您的回答。这有助于删除第二个窗口。但由于某种原因,Style 并不能完全正常工作。我阅读了 ttk 文档并做了同样的事情,但它不起作用。我表示该按钮是橙色的,但它是灰色的。如何解决?抱歉这个愚蠢的问题。
    • 嘿@rdbrg,我建议你使用tk Buttons。与ttk 相比,它更容易定制。
    • 既然我的解决方案解决了您的为什么我连接样式时会出现两个窗口?请接受这个答案,因为这将有助于社区识别正确的解决方案。干杯!
    • 嘿@rdbrg,我更新了代码添加style.theme_use('clam') 以制作按钮orange
    • 当然。但我正在学习 ttk。您的建议“Tk 按钮会更容易”是可以理解的,但对我的学习没有帮助。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 2014-09-16
    • 2017-06-19
    • 2014-07-25
    • 2023-03-23
    相关资源
    最近更新 更多