1

我尝试设计一个 tkinter gui,首先这个 gui 不容易修改。我为我使用的小部件达到了我想要的样式,除了一个想法。我锁定了 ttk.OptionMenu 小部件中菜单周围的浅灰色边框。

如何去除或着色呢?

Windows 10 上的 Python 3x

这里只是代码的相关部分。

一些常见的样式:

self.style = ttk.Style(self)
self.style.theme_use("clam")

self.font12 = tkFont.Font(family="helvetica", size=12)
self.font14 = tkFont.Font(family="helvetica", size=14)

master.option_add("*font", self.font12)
master.option_add("*background", self.bgc)
master.option_add("*foreground", self.fgc)

小部件的样式:

master.option_add("*Menu.background", self.dbgc)

self.style.configure("TMenubutton", relief=tk.FLAT, font=self.font14, bd=0, highlightthickness=0,
        arrowcolor="#909090", foreground=self.dfgc, background=self.dbgc)
self.style.map("TMenubutton",
        background=[('disabled', self.dbgc),('pressed', self.abgc), ('active', self.abgc)],
        foreground=[('disabled', "#707070")])

OptionMenu 和是菜单:

om3 = ttk.OptionMenu(self.top_frame, self.var_quality,
    qualities['u'], *['UltraQuality','High','Normal','Preview'])
om3.pack(side=tk.LEFT, padx=4)
om3['width'] = 10
om3['menu'].configure(relief=tk.FLAT, font=self.font12,
    borderwidth=1, activeborderwidth=5,  activeforeground=self.afgc,
    activebackground=self.abgc, selectcolor=self.fgc)

结果:

OptionMenu 和菜单

谢谢。

添加:

@stovfl对不起,在我的第一篇文章中,我删除了这些行对结果没有任何影响,所以,你的新评论让我明白我不应该这样做。因此,注释行没有任何影响。

完整地说,正如您在原始帖子中看到的那样,“highlightsickness|borderwidth|activeborderwidth”已经被使用。

并且要真正完整 highlightthickness=0 看起来也没有效果,但在我的第一篇文章中没有评论。

self.style.configure("TMenubutton", relief=tk.FLAT, font=self.font14, bd=0, highlightthickness=0,
    # bordercolor=self.dbgc, focusthickness=0, focuscolor=self.dbgc,
    arrowcolor="#909090", foreground=self.dfgc, background=self.dbgc)
self.style.map("TMenubutton",
    background=[('disabled', self.dbgc),('pressed', self.abgc), ('active', self.abgc)],
    foreground=[('disabled', "#707070")],
    # focuscolor=[('disabled', self.dbgc), ('active', self.dbgc)])

# self.style.configure("TMenu", highlightthickness=0, bordercolor=self.dbgc, 
#     focusthickness=0, focuscolor=self.dbgc)
# self.style.map("TMenu",
#     highlightbackground=[('disabled', self.dbgc), ('active', self.dbgc)],
#     highlightcolor=[('disabled', self.dbgc), ('active', self.dbgc)],
#     focuscolor=[('disabled', self.dbgc), ('active', self.dbgc)])

在这里,每条注释行都会产生一个错误。

om3 = ttk.OptionMenu(self.top_frame, self.var_quality,
    qualities['u'], *['UltraQuality','High','Normal','Preview'])
om3.pack(side=tk.LEFT, padx=4)
om3['width'] = 10
om3['menu'].configure(relief=tk.FLAT, font=self.font12,
    # focuscolor=self.dbgc,
    # focusthickness=0,
    # bordercolor=self.dbgc,
    # highlightthickness=0,
    borderwidth=1, activeborderwidth=5,  activeforeground=self.afgc,
    activebackground=self.abgc, selectcolor=self.fgc)

谢谢。

添加:

这个程序不是用于学习的私人测试,它是针对我的工作的。这是我第一次尝试使用 tkinter。另外,我以前从来不需要尝试在 python 中做 gui。该程序运行良好,并完成了我应该实施的所有工作。这里的重点只是这个奇怪边框的美学和造型细节。我总是浏览 Stackoverflow,它是丰富的信息来源,这就是为什么我决定创建一个帐户并在这里发布我的第一个问题。

谢谢。

@stovfl是的,我也希望在这个选项中,但是borderwidth 和activeborderwidth 是菜单的选项,会影响菜单的一些内边框,而不是外边框。

为了显示边框宽度的效果,我在 50 处使用了夸大值:

在此处输入图像描述


@stovfl打印结果。

{'activebackground': ('activebackground', 'activeBackground', 'Foreground', <string object: 'SystemHighlight'>, <string object: '#606060'>),
'activeborderwidth': ('activeborderwidth', 'activeBorderWidth', 'BorderWidth', '0', 5),
'activeforeground': ('activeforeground', 'activeForeground', 'Background', <string object: 'SystemHighlightText'>, <string object: '#ffffff'>),
'background': ('background', 'background', 'Background', 'SystemMenu', <string object: '#353535'>),
'bd': ('bd', '-borderwidth'),
'bg': ('bg', '-background'),
'borderwidth': ('borderwidth', 'borderWidth', 'BorderWidth', '0', 1),
'disabledforeground': ('disabledforeground', 'disabledForeground', 'DisabledForeground', <string object: 'SystemDisabledText'>, <string object: '#606060'>),
'fg': ('fg', '-foreground'),
'foreground': ('foreground', 'foreground', 'Foreground', 'SystemMenuText', <string object: '#dddddd'>),
'relief': ('relief', 'relief', 'Relief', 'flat', <string object: 'flat'>),
'selectcolor': ('selectcolor', 'selectColor', 'Background', <string object: 'SystemMenuText'>, <string object: '#dddddd'>),

在此处输入图像描述
在我的环境中,边框颜色是black并且borderwidth=0没有显示边框。


4

0 回答 0