Getting the selected value from combobox in Tkinter

前端 未结 2 1689
旧巷少年郎
旧巷少年郎 2020-12-06 12:47

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 select

相关标签:
2条回答
  • 2020-12-06 12:57
    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()
    
    0 讨论(0)
  • 2020-12-06 13:08

    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.

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

    def justamethod (self, event): 
    

    After making these changes the code is working well.

    0 讨论(0)
提交回复
热议问题