Skip to content Skip to sidebar Skip to footer

Get Combobox Value In Python

I'm developing an easy program and I need to get the value from a Combobox. It is easy when the Combobox is in the first created window but for example if I have two windows and th

Solution 1:

You cannot have two Tk() windows. one must be Toplevel.

To get the variable you can do box_value.get()

Example of a drop down box :

classTableDropDown(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 print(self.current_table.get())

Solution 2:

from tkinter import *
from tkinter import ttk
from tkinter import messagebox

root = Tk()

root.geometry("400x400")
# Length and width window :D

cmb = ttk.Combobox(root, width="10", values=("prova","ciao","come","stai"))
# to create checkbox# cmb = Combobox#now we create simple function to check what user select value from checkboxdefcheckcmbo():

     if cmb.get() == "prova":
         messagebox.showinfo("What user choose", "you choose prova")

    # if user select prova show this message elif cmb.get() == "ciao":
        messagebox.showinfo("What user choose", "you choose ciao")

     # if user select ciao show this message 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()

Post a Comment for "Get Combobox Value In Python"