亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Python Tkinter中綁定ComboBox的ListBox

如何在Python Tkinter中綁定ComboBox的ListBox

莫回無 2023-12-26 15:00:12
我希望進行一個綁定,允許我根據用戶按下的鍵放置某個值。這是我執行此操作的示例代碼:from tkinter import *from tkinter import ttkv=Tk()combo = ttk.Combobox(values=['item1','item2','item3','item4'], state='readonly')combo.current(0)combo.pack()def function(Event):    if(Event.char in '1234'):        combo.set(f'item{Event.char}')combo.bind('<Key>', function)v.mainloop()他們會告訴我“如果這有效,你問這個做什么?” 事實證明,如果部署了組合框,綁定就會停止工作。問題如何解決?我知道這部分問題不應該問,因為它與所討論的問題無關。但我想問一下,如果這個問題有問題,或者拼寫錯誤,或者其他什么,請告知?!叭绾翁岢鲆粋€好問題”的頁面對我沒有幫助,因為從我的角度來看,我按照他們所說的去做一切。我盡力使此處所寫的內容盡可能詳細且易于理解。希望您的理解,謝謝。
查看完整描述

1 回答

?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊


from tkinter import *

from tkinter import ttk


v=Tk()


# I create two test combobox

for _ in range(2):

? ? combo = ttk.Combobox(values=['item1','item2','item3','item4'], state='readonly')

? ? combo.current(0)

? ? combo.pack()


# I create a test entry to test if the function correctly recognizes when it should be executed

entrada = Entry()

entrada.pack()


def function(Event):

? ? """

? ? If Event.widget is a str and ends with ".popdown.f.l" I consider it to be the Listbox,

? ? I get the path of the Combobox it belongs to and convert it to a widget.

? ? Afterwards, I set the value of Event.widget to that of the supposed combobox.

? ? """

? ? if(isinstance(Event.widget, str) and Event.widget.endswith(".popdown.f.l")):

? ? ? ? Event.widget = v._nametowidget(Event.widget[:-len(".popdown.f.l")])


? ? ? ??

? ? # If Event.widget is not a Combobox, it stops the execution of the function.

? ? if(not isinstance(Event.widget, ttk.Combobox)):

? ? ? ? return


? ? if(Event.char in '1234'):

? ? ? ? Event.widget.set(f'item{Event.char}')


v.bind_all('<Key>', function)

v.mainloop()


查看完整回答
反對 回復 2023-12-26
  • 1 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號