首页 > 解决方案 > 有没有办法确保在选择子小部件时突出显示的父小部件保持突出显示

问题描述

我有一个单选按钮,可以突出显示相应的 LabelFrame。

每个 LabelFrame 都有一个 Entry 小部件作为子小部件。When the Entry widget is selected to type in some input, the parent LabelFrame loses the given highlightbackground color (from cyan to gray) but keeps the same highlightthickness.

有没有办法保持给定的高亮背景颜色?

(windows 7 64,pycharm 2019.2)

提前致谢。

from tkinter import *
from tkinter import ttk
import tkinter as tk


class doSomeStuff(Tk):
    def __init__(self):
        Tk.__init__(self)

        self.radioBtnVar = StringVar()  # radiobutton variable

        # main canvas

        pwdCanvas = tk.Canvas(self, bd=0, highlightthickness=0)
        pwdCanvas.pack()

        # choiceLabelFrame
        choiceLabelFrame = ttk.LabelFrame(pwdCanvas, text='Choice LabelFrame (ttk)')
        choiceLabelFrame.grid(column=0, row=11, columnspan=2, sticky='nsew')

        # radio button 1
        rbtn1 = ttk.Radiobutton(choiceLabelFrame, text='A', variable=self.radioBtnVar, value='PCG', command=self.colorLabels)
        rbtn1.pack(side='left')

        # radio button 2
        rbtn2 = ttk.Radiobutton(choiceLabelFrame, text='B', variable=self.radioBtnVar, value='UG', command=self.colorLabels)
        rbtn2.pack(side='right')

        # LabelFrame1, left side
        self.LabelFrame1 = tk.LabelFrame(pwdCanvas, text="LabelFrame 1 (tk)", bd=0)  # I use tk to have access to the 'highlightbackground' option
        self.LabelFrame1.grid(column=0, row=12, sticky='nsew', padx=3, pady=3)

        entry1Label = ttk.Label(self.LabelFrame1, text='Entry 1')
        entry1Label.grid(column=0, row=11, sticky='w')

        self.labelEntry1 = ttk.Entry(self.LabelFrame1, state='disabled')
        self.labelEntry1.grid(column=1, row=11, sticky='w')

        # LabelFrame2, right side
        self.LabelFrame2 = tk.LabelFrame(pwdCanvas, text="LabelFrame 2 (tk)", bd=0)
        self.LabelFrame2.grid(column=1, row=12, sticky='nw', padx=3, pady=3)

        entry2Label = ttk.Label(self.LabelFrame2, text='Entry 2')
        entry2Label.grid(column=0, row=0)

        labelEntry2 = ttk.Entry(self.LabelFrame2, state='disabled')
        labelEntry2.grid(column=1, row=0)

    def colorLabels(self):  # activates and highlights the chosen option
        if self.radioBtnVar.get() == 'PCG':
            for child in self.LabelFrame1.winfo_children():
                child.config(state='enabled')
            self.LabelFrame1.config(highlightbackground='cyan', highlightthickness=2)

            for child in self.LabelFrame2.winfo_children():
                child.config(state='disabled')
            self.LabelFrame2.config(highlightthickness=0)

        elif self.radioBtnVar.get() == 'UG':
            for child in self.LabelFrame2.winfo_children():
                child.config(state='enabled')
            self.LabelFrame2.config(highlightbackground='cyan', highlightthickness=2)

            for child in self.LabelFrame1.winfo_children():
                child.config(state='disabled')
            self.LabelFrame1.config(highlightthickness=0)


if __name__ == "__main__":
    app = doSomeStuff()
    app.mainloop()

标签: python-3.xtkinterhighlight

解决方案


我找到了获得我想要的东西的方法。

    def colorLabels(self):
        if self.radioBtnVar.get() == 'PCG':
            for child in self.LabelFrame1.winfo_children():
                child.config(state='enabled')
            self.LabelFrame1.config(highlightbackground='cyan', highlightcolor='cyan', highlightthickness=2)

            for child in self.LabelFrame2.winfo_children():
                child.config(state='disabled')
            self.LabelFrame2.config(highlightthickness=0)

        elif self.radioBtnVar.get() == 'UG':
            for child in self.LabelFrame2.winfo_children():
                child.config(state='enabled')
            self.LabelFrame2.config(highlightbackground='cyan', highlightcolor='cyan', highlightthickness=2)

            for child in self.LabelFrame1.winfo_children():
                child.config(state='disabled')
            self.LabelFrame1.config(highlightthickness=0)

我只是添加了'highlightcolor ='青色''。正如此处effbot.org所解释的:

  • 当小部件没有焦点时使用“highlightbackground”。

  • 当小部件具有焦点时使用“highlightcolor”。

这样我的小部件即使不在焦点上也能保持其突出显示的轮廓。


推荐阅读