如何使python tkinter匹配按钮

2024-09-30 01:21:50 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么tkinter.按钮()显示为一些古老的操作系统风格的按钮,而messagebox类似tkinter.messagebox.showinfo()使用当前版本的操作系统提供了“确定”按钮?在

我的操作系统是Windows。不确定这个问题是否存在于Mac操作系统上,但是不管怎样,使用我工具的人都是在Windows上。在

我发现的一个示例代码片段here显示了按钮的不同之处。在

图像

enter image description here

问题

有没有办法tkinter.按钮()看起来像messagebox中的按钮,似乎使用了当前的操作系统风格?在

代码

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

Tags: 代码fromimportmessageheretitle风格tkinter
2条回答

你可以用tkinter.ttk公司它为您的TK小部件提供了一个现代OS风格的主题。来自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:

enter image description here

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

BeforeAfter

doc

相关问题 更多 >

    热门问题