【问题标题】:How to make python tkinter match button to OS如何使 python tkinter 匹配按钮到操作系统
【发布时间】:2019-06-26 09:33:36
【问题描述】:

为什么 tkinter.Button() 显示为一些古老的操作系统风格的按钮,而像 tkinter.messagebox.showinfo() 这样的消息框在使用当前版本的操作系统时带有一个确定按钮?

我的操作系统是 Windows。不确定 Mac OS 上是否存在此问题,但无论哪种方式,使用我的工具的人都是在 Windows 上。

我发现here 的代码示例 sn-p 显示了按钮的不同之处。

图片

问题

有没有办法让 tkinter.Button() 看起来像消息框中的按钮,似乎使用当前的操作系统样式?

代码

from tkinter import *
from tkinter import messagebox
window = Tk()
window.title("Welcome to LikeGeeks app")
window.geometry('350x200')
def clicked():
    messagebox.showinfo('Message title', 'Message content')
btn = Button(window,text='Click here', command=clicked)
btn.grid(column=0,row=0)
window.mainloop()

【问题讨论】:

    标签: python button tkinter operating-system


    【解决方案1】:

    使用tkinter.ttk获取主题版本

    from tkinter.ttk import *
    from tkinter import messagebox
    window = Tk()
    window.title("Welcome to LikeGeeks app")
    window.geometry('350x200')
    def clicked():
        messagebox.showinfo('Message title', 'Message content')
    btn = Button(window,text='Click here', command=clicked)
    btn.grid(column=0,row=0)
    window.mainloop()
    

    doc

    【讨论】:

    • 谢谢!这个例子清楚地说明了如何在我自己的脚本中重用 ttk.Button。
    【解决方案2】:

    您可以使用 tkinter.ttk,它为您的 TK 小部件提供现代操作系统风格的主题。来自docs 的示例:

    from tkinter import ttk
    import tkinter
    
    root = tkinter.Tk()
    
    ttk.Style().configure("TButton", padding=6, relief="flat",
       background="#ccc")
    
    btn = ttk.Button(text="Sample")
    btn.pack()
    
    root.mainloop()
    #Output:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-15
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多