Python 从 Tkinter 的组合框中获取选定的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31264522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:43:25  来源:igfitidea点击:

Getting the selected value from combobox in Tkinter

pythoncomboboxtkinter

提问by Dania

I've made a simple combobox in python using Tkinter, I want to retrieve the value selected by the user. After searching, I think I can do this by binding an event of selection and call a function that will use something like box.get(), but this is not working. When the program starts the method is automatically called and it doesn't print the current selection. When I select any item from the combobox no method gets called. Here is a snippet of my code:

我使用 Tkinter 在 python 中制作了一个简单的组合框,我想检索用户选择的值。搜索后,我想我可以通过绑定一个选择事件并调用一个将使用 box.get() 之类的函数来做到这一点,但这不起作用。当程序启动时,该方法会自动调用,并且不会打印当前选择。当我从组合框中选择任何项目时,不会调用任何方法。这是我的代码片段:

    self.box_value = StringVar()
    self.locationBox = Combobox(self.master, textvariable=self.box_value)
    self.locationBox.bind("<<ComboboxSelected>>", self.justamethod())
    self.locationBox['values'] = ('one', 'two', 'three')
    self.locationBox.current(0)

This is the method that is supposed to be called when I select an item from the box:

这是当我从框中选择一个项目时应该调用的方法:

def justamethod (self):
    print("method is called")
    print (self.locationBox.get())

Can anyone please tell me how to get the selected value?

谁能告诉我如何获得选定的值?

EDIT: I've corrected the call to justamethod by removing the brackets when binding the box to a function as suggested by James Kent. But now I'm getting this error:

编辑:我已经按照 James Kent 的建议,在将框绑定到函数时删除了括号,从而更正了对 justmethod 的调用。但现在我收到此错误:

TypeError: justamethod() takes exactly 1 argument (2 given)

类型错误:justamemethod() 只需要 1 个参数(给出 2 个)

EDIT 2: I've posted the solution to this problem.

编辑 2:我已经发布了这个问题的解决方案。

Thank You.

谢谢你。

采纳答案by Dania

I've figured out what's wrong in the code.

我已经弄清楚代码有什么问题了。

First, as James said the brackets should be removed when binding justamethod to the combobox.

首先,正如詹姆斯所说,在将 justmethod 绑定到组合框时应该删除括号。

Second, regarding the type error, this is because justamethod is an event handler, so it should take two parameters, self and event, like this,

其次,关于类型错误,这是因为justmethod是一个事件处理器,所以它应该带两个参数,self和event,像这样,

def justamethod (self, event): 

After making these changes the code is working well.

进行这些更改后,代码运行良好。

回答by Machine

from tkinter import ttk
from tkinter import messagebox
from tkinter import Tk



root = Tk()

root.geometry("400x400")
#^ width - heghit window :D


cmb = ttk.Combobox(root, width="10", values=("prova","ciao","come","stai"))
#cmb = Combobox

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", "Prices"])
        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() == "prova":
         messagebox.showinfo("What user choose", "you choose prova")

    elif cmb.get() == "ciao":
        messagebox.showinfo("What user choose", "you choose ciao")

    elif cmb.get() == "come":
        messagebox.showinfo("What user choose", "you choose come")

    elif cmb.get() == "stai":
        messagebox.showinfo("What user choose", "you choose stai")

    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="Get Value",command=checkcmbo)
btn.place(relx="0.5",rely="0.1")

root.mainloop()