简体   繁体   English

创建下拉列表和执行所需操作所需的Python Tkinter帮助

[英]Python Tkinter help needed to create dropdowns and perform required operation

I can create the below option menu but my question is when I select any value from dropdown it should display value stored in a variable 'a' for c[4] , value stored in a variable 'b' for C[5] and for all others it should show text "none available". 我可以创建下面的选项菜单,但是我的问题是,当我从下拉列表中选择任何值时,它应该为c [4]显示存储在变量'a'中的值,为C [5]显示存储在变量'b'中的值。其他所有内容应显示“无可用”文字。

I have tried to cut short as far as possible. 我已经尽力缩短了。

        master = Tk()
    variable = StringVar(master)
variable.set("Summary")
w = OptionMenu(master, variable, c[0], c[1], c[3],c[4],c[5],c[6],c[7])
w.pack()
mainloop()

How should I print the output do I need to use 我应该如何使用我的输出输出

'def ok():print("value is", var.get())'

and then a button: 然后一个按钮:

button = Button(master, text="OK", command=ok)

Use the Tkinter OptionMenu widget, and create a text variable to track the value of the selection. 使用Tkinter OptionMenu小部件,并创建一个文本变量以跟踪选择的值。 You can call .get() on the variable to find out what's selected. 您可以在变量上调用.get()来找出所选内容。 ie. 即。

s = tk.StringVar()
tk.OptionMenu(window, s, c[0], c[1]...  )

def function(x):
    # use x to find data
    ...

button = tk.Button(window, text='Button', command= lambda x=s.get() : function(x) )

You need a button aswell, so keep the one you've got and the command should use .get() to get the dropdown selection, then you can use that info to find your data. 您还需要一个按钮,因此请保留一个按钮,该命令应使用.get()获取下拉菜单,然后您可以使用该信息来查找数据。

The effbot docs are quite good for explaining how to use tkinter widgets. effbot文档非常适合解释如何使用tkinter小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM