Reputation: 908
To defocus the ComboBox widgets, I use the "Defocus" custom function written in my example:
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()
it works but not at all, it has a "bad behaviour" for my point of view. the text in the ComboBox widgets became bold and I don't like it (see my attached GIF). I have two questions:
how can I improve my custom "Defocus" function to take off the "bold option" in the ComboBox widgets?
is there a way to change the default style of the ComboBox widgets to delete the focus and the bold text options? in this way, I can avoid to use my custom function every times I have to use a ComboBox widget.
Upvotes: 3
Views: 633
Reputation: 554
Combobox uses bold text whenever it's entry is focused. So a solution would be to use, another entry and shift focus towards it.
So i created a dummy Entry called dump
.this dump is hidden through place_forget()
.
How to shift focus?
for that run this steps whenever a item is selected <<ComboboxSelected>>
parent.focus()
)set()
)focus()
)select_range()
To know the difference i have included Normal Combobox 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()
Edit: Now it doesn't depend on the parent element. (refer to that CBox
alone)
Upvotes: 1