禁用带有条件的Tkiner组合框的某些选项

时间:2019-09-23 13:27:35

标签: python tkinter

我正在尝试构建一个简单的gui,它具有2个comboboxes(例如combo1combo2)。

我希望在选择combo1的某些特定值时禁用/停用combo2的某些值。

以下是示例代码:

import ttk


root=tk.Tk()
c1_val=['0','1','2','3']
c2_val=['a','b','c']

combo1=ttk.Combobox(root,values=c1_val)
combo1.set("Select No")
combo1.place(relx=0.01,rely=0.4)

combo2=ttk.Combobox(root,values=c2_val)
combo2.set("Select No")
combo2.place(relx=0.01,rely=0.5)




var1=IntVar()
check1=tk.Checkbutton(root,text="Select1", variable=var1)
check1.place(relx=0.01,rely=0.7)

var2=IntVar()
check2=tk.Checkbutton(root,text="Select2", variable=var2)
check2.place(relx=0.4,rely=0.7)



root.mainloop()

在上面的脚本中,我希望combo1 = ['0','1']的值为combo2= ['a']

我在这里可以实现什么功能?

1 个答案:

答案 0 :(得分:1)

以下是 weightControllerService.$inject = ['$timeout', '$q']; function weightControllerService($timeout, $q) { let service = this; service.nameCheck = Name => { let deferred = $q.defer(); let result = { error: false, message: '' }; $timeout(() => { if (Name.toLowerCase().indexOf('cookie') === -1) { deferred.resolve(result); } else { result.message = 'dont buy cookies'; result.error = true; deferred.reject(result); } }, 2000); return deferred.promise; }; } })(); 的值set的示例:

Combobox

将此逻辑应用于您的代码,它应该可以工作

更新

要重新分配特定import tkinter as tk from tkinter import ttk def setValue(event): print(combo.set('January')) app = tk.Tk() app.geometry('400x100') labelTop = tk.Label(app, text = "Choose your favourite month") labelTop.grid(column=0, row=0) combo = ttk.Combobox(app, values=[ "January", "February", "March", "April", "May"]) combo.grid(column=0, row=1) combo.current(1) combo.bind("<<ComboboxSelected>>", setValue) app.mainloop() 的值,请尝试以下操作: (使用与上述相同的示例)

Combobox