簡體   English   中英

Tkinter - 如何刪除 ComboBox 小部件中的粗體文本和焦點?

[英]Tkinter - How can I delete the bold text and the focus in the ComboBox widgets?

為了散焦 ComboBox 小部件,我使用了示例中編寫的“Defocus”自定義函數:

from tkinter import *
from tkinter import ttk

def Defocus(event):
    event.widget.master.focus_set()

parent=Tk()
parent.geometry("530x280+370+100")
parent.title("TEST")
parent.configure(background="#f0f0f0")
parent.minsize(485, 280)

SV=StringVar()
SV.set("I hate to see the bold text..")
MenuComboBox=ttk.Combobox(parent, state="readonly", values=("I hate to see the bold text..", "ciao", "hola", "hello"), textvariable=SV, width=50)
MenuComboBox.bind("<FocusIn>", Defocus)
MenuComboBox.place(x=20, y=20)

SV2=StringVar()
SV2.set("I hate to see the bold text..")
MenuComboBox2=ttk.Combobox(parent, state="readonly", values=("I hate to see the bold text..", "ciao", "hola", "hello"), textvariable=SV2, width=50)
MenuComboBox2.bind("<FocusIn>", Defocus)
MenuComboBox2.place(x=20, y=60)

parent.mainloop()

它有效但根本不起作用,在我看來,它具有“不良行為”。 ComboBox 小部件中的文本變得粗體,我不喜歡它(請參閱我附加的 GIF)。 我有兩個問題:

  1. 如何改進我的自定義“散焦”功能以取消 ComboBox 小部件中的“粗體選項”?

  2. 有沒有辦法更改 ComboBox 小部件的默認樣式以刪除焦點和粗體文本選項? 這樣,我可以避免每次必須使用 ComboBox 小部件時都使用我的自定義函數。

在此處輸入圖片說明

組合框在輸入焦點時使用粗體文本。 因此,解決方案是使用另一個條目並將重點轉向它。
所以我創建了一個名為dump的虛擬條目。這個轉儲是通過place_forget()隱藏的。
如何轉移焦點? 對於該項目被選中<<ComboboxSelected>>來運行此步驟

  • 聚焦根窗口( parent.focus()
  • 在條目中輸入一些內容( set()
  • 聚焦它( focus()
  • 選擇它select_range()

要知道差異,我已經包括了普通組合MenuComboBox2

from tkinter.ttk import Combobox
from tkinter import *
class CBox(Frame):
    def __init__(self,parent,variable):
        super().__init__(parent)
        self.SV=variable
        self.Box=Combobox(self,state="readonly", values=("I hate to see the bold text..", "ciao", "hola", "hello"), textvariable=self.SV, width=50)
        self.Box.bind('<<ComboboxSelected>>',self.doThat)
        self.fvar=StringVar()
        self.fake=Entry(self,text='test',textvariable=self.fvar)
        self.arrange()
    def arrange(self):
        self.Box.pack()
        self.fake.pack()
        self.fake.pack_forget()
    def doThat(self,*args):
        self.focus()
        self.fvar.set('Hello')
        self.fake.focus()
        self.fake.select_range(0,'end')
root=Tk()
SV=StringVar()
def Defocus(event):
    event.widget.master.focus_set()
diff=Combobox(root,state="readonly", values=("I hate to see the bold text..", "ciao", "hola", "hello"), width=50)
diff.pack()
diff.bind("<FocusIn>", Defocus)
a=CBox(root,SV)
a.pack()

root.mainloop()

編輯:現在它不依賴於父元素。 (單獨參考那個CBox

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM