gpt4 book ai didi

python - 如何进行 ttk.Combobox 回调

转载 作者:太空狗 更新时间:2023-10-29 20:47:14 25 4
gpt4 key购买 nike

我想知道当用户从下拉列表中选择一个项目时,是否有一种方法可以从 ttk.Combobox 调用回调。我想查看单击某个项目时组合框的值是什么,以便我可以在给定组合框键的情况下显示关联的字典值。

import Tkinter
import ttk

FriendMap = {}
UI = Tkinter.Tk()
UI.geometry("%dx%d+%d+%d" % (330, 80, 500, 450))
UI.title("User Friend List Lookup")

def TextBoxUpdate():
if not FriendListComboBox.get() == "":
FriendList = FriendMap[FriendListComboBox.get()]
FriendListBox.insert(0,FriendMap[FriendListComboBox.get()])`

#Imports the data from the FriendList.txt file
with open("C:\Users\me\Documents\PythonTest\FriendList.txt", "r+") as file:
for line in file:
items = line.rstrip().lower().split(":")
FriendMap[items[0]] = items[1]

#Creates a dropdown box with all of the keys in the FriendList file
FriendListKeys = FriendMap.keys()
FriendListKeys.sort()
FriendListComboBox = ttk.Combobox(UI,values=FriendListKeys,command=TextBoxUpdate)`

最后一行显然不起作用,因为组合框没有“命令”,但我不太确定我需要在这里做什么才能让它起作用。任何帮助将不胜感激。

最佳答案

您可以绑定(bind)到 <<ComboboxSelected>>只要组合框的值发生变化,就会触发该事件。

def TextBoxUpdate(event):
...
FriendListComboBox.bind("<<ComboboxSelected>>", TextBoxUpdate)

关于python - 如何进行 ttk.Combobox 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35209522/

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