gpt4 book ai didi

python - Tkinter - 如何删除 ComboBox 小部件中的粗体文本和焦点?

转载 作者:行者123 更新时间:2023-12-04 03:47:14 29 4
gpt4 key购买 nike

为了散焦 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 小部件时都使用我的自定义函数。

enter image description here

最佳答案

组合框在其输入项获得焦点时使用粗体文本。所以一个解决方案是使用另一个条目并将焦点转移到它上面。
所以我创建了一个名为 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)

关于python - Tkinter - 如何删除 ComboBox 小部件中的粗体文本和焦点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64961292/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com