简体   繁体   English

如何在tkinter ttk中获得无边框效果?

[英]how get no border effect in tkinter ttk?

在此处输入图片说明

To get button no border effect in tkinter tk I used to set borderwidth=0 . 为了使按钮在borderwidth=0 tk中没有边框效果,我曾经设置borderwidth=0 Button will merge into background. 按钮将合并为背景。 But I can't get same effect in tkinter ttk. 但是我在tkinter ttk中无法获得相同的效果。 I set borderwidth=0 in style. 我在样式中设置borderwidth=0 Button always have borderwidth. 按钮始终具有borderwidth。 I don't know why? 不知道为什么

What you want can be achieved by setting the button relief to flat or borderwidth to 0 using a ttk style. 您可以通过使用ttk样式将按钮浮雕设置为flat或borderwidth设置为0来实现。 However, some ttk themes don't take into account these style settings, and one of them is the default theme in Windows. 但是,某些ttk主题没有考虑这些样式设置,其中之一是Windows中的默认主题。 Setting the theme to 'clam' or 'alt' should solve your problem. 将主题设置为“蛤”或“替代”应该可以解决您的问题。

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

b1 = tk.Button(root, text='tk.Button', borderwidth=0)
b1.pack()

s = ttk.Style(root)
s.theme_use('clam')
s.configure('flat.TButton', borderwidth=0)
# s.configure('flat.TButton', relief='flat') gives the same result

b2 = ttk.Button(root, style='flat.TButton', text='ttk.Button')
b2.pack()

root.mainloop()

You can't remove the border on windows or osx. 您无法在Windows或osx上删除边框。 The whole point of using ttk buttons on those platforms is to get native widgets. 在这些平台上使用ttk按钮的全部目的是获取本机窗口小部件。 If you want a button without a border, use the standard tk button. 如果您想要一个没有边框的按钮,请使用标准的tk按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM