如果组合框值为selectek Tkinter Python3,则显示标签
当我在组合框中选择某个值时,我需要显示/添加标签和文本框。我发现这一点,它有帮助,但我仍然没有得到理想的输出 其目的是当我在组合框中选择单个标记的值时,显示标签vlan和文本框。 这是我的代码:如果组合框值为selectek Tkinter Python3,则显示标签,python,python-3.x,tkinter,combobox,Python,Python 3.x,Tkinter,Combobox,当我在组合框中选择某个值时,我需要显示/添加标签和文本框。我发现这一点,它有帮助,但我仍然没有得到理想的输出 其目的是当我在组合框中选择单个标记的值时,显示标签vlan和文本框。 这是我的代码: from tkinter import ttk from tkinter import messagebox from tkinter import Tk from tkinter import Label, Entry, StringVar root = Tk() root.title('Tra
from tkinter import ttk
from tkinter import messagebox
from tkinter import Tk
from tkinter import Label, Entry, StringVar
root = Tk()
root.title('Traffic generation')
root.geometry('250x250')
ttk.Label(root, text = "Traffic generation",
background = 'light blue', foreground ="white",
font = ("Times New Roman", 15)).grid(row = 0, column = 1, sticky='NESW')
cmb = ttk.Combobox(root, width="10", values=(' Untagged',' Single tagged',' Double tagged'))
def handler(event):
current = cmb.current()
if current == ' Single tagged':
labelText=StringVar()
labelText.set("VLAN")
labelDir=Label(root, textvariable=labelText, height=4)
labelDir.grid(row=2,column=0,sticky='s')
directory=StringVar(None)
dirname=Entry(root,textvariable=directory,width=50)
dirname.grid(row=2,column=1,sticky='e')
#cmb = Combobox
cmb.bind('<<ComboboxSelected>>', handler)
cmb.grid(row=1,column=0,sticky='s')
class TableDropDown(ttk.Combobox):
def __init__(self, parent):
self.current_table = tk.StringVar() # create variable for table
ttk.Combobox.__init__(self, parent)# init widget
self.config(textvariable = self.current_table, state = "readonly", values = ["Customers", "Pets", "Invoices"])
self.current(0) # index of values for current table
self.place(x = 50, y = 50, anchor = "w") # place drop down box
def checkcmbo():
if cmb.get() == " Untagged":
messagebox.showinfo("What user choose", "you chose Untagged")
elif cmb.get() == " Single tagged":
messagebox.showinfo("What user choose", "you choose Single tagged")
elif cmb.get() == " Double tagged":
messagebox.showinfo("What user choose", "you choose Double tagged")
elif cmb.get() == "":
messagebox.showinfo("nothing to show!", "you have to be choose something")
cmb.place(relx="0.1",rely="0.1")
btn = ttk.Button(root, text="Select mode",command=checkcmbo)
btn.place(relx="0.5",rely="0.1")
root.mainloop()
问题是Combobox.current将返回索引而不是值
有两种解决方案:
将if current==“Single taged”:更改为if current==1。
将if current==“Single taged”:更改为if cmb.get==“Single taged”:。
否,当我选择/点击单个标记的项目时,我需要显示标签