Home > Enterprise >  Print specific value in TKinter from list
Print specific value in TKinter from list

Time:04-11

I am using Tkinter/Python to get the selected option on the window when the button is pressed. On clicking the button - only the value from list should get printed. As of now, it's printing any value typed into combobox. Any help/suggestion will be appreciated.

from tkinter import *
from tkinter import ttk
class Run:
    def __init__(self, master):
        
        self.lst = ["Apples", "Oranges", "Pears", "Grapes"]
        self.master = master
        self.toplevels = 0
        
        master.title("CB")
        master.geometry("300x200")
        label = Label(master, text = "ABC")
        label.pack()
        
        self.combo_box = ttk.Combobox(master,value=self.lst)
        self.combo_box.set('')
        self.combo_box.pack()

        self.combo_box.bind('<KeyRelease>', self.search)

        button = Button(master, text="btn", command=self.make_new)#self.make_new)
        button.pack()
            
    def make_new(self):
        if not self.toplevels:
            #new = tk.Toplevel(self.master)
            my_label = Label(self.master, text=self.combo_box.get(), font=("Helvetica", 14))#, fg="grey")
            my_label.pack(padx=10, pady=10)
            self.toplevels  = 1
            
    def search(self, event):
        value = event.widget.get()
        if value == '':
            self.combo_box['values'] = self.lst
        else:
            data = []

            for item in self.lst:
                if value.lower() in item.lower():
                    data.append(item)
            self.combo_box['values'] = data

master1 = Tk()
i = Run(master1)
master1.mainloop()

CodePudding user response:

The answer is simple. You just have to a condition which will detect if the text of the combo box is in the list or not. That condition would be: if self.combo_box.get() in self.lst:. And for the corrected code:

from tkinter import *
from tkinter import ttk


class Run:
    def __init__(self, master):

        self.my_label = Label(master, text="")
        self.lst = ["Apples", "Oranges", "Pears", "Grapes"]
        self.master = master
        self.toplevels = 0

        master.title("CB")
        master.geometry("300x200")
        label = Label(master, text="ABC")
        label.pack()

        self.combo_box = ttk.Combobox(master, value=self.lst)
        self.combo_box.set('')
        self.combo_box.pack()

        self.combo_box.bind('<KeyRelease>', self.search)

        button = Button(master, text="btn", command=self.make_new)  # self.make_new)
        button.pack()

    def make_new(self):
        if not self.toplevels:
            # new = tk.Toplevel(self.master)
            if self.combo_box.get() in self.lst:
                self.my_label.config(text=self.combo_box.get(), font=("Helvetica", 14))  # , fg="grey")
            self.my_label.pack(padx=10, pady=10)
            self.toplevels  = 1

    def search(self, event):
        value = event.widget.get()
        if value == '':

                self.combo_box['values'] = self.lst
        else:
            data = []

            for item in self.lst:
                if value.lower() in item.lower():
                    data.append(item)
            self.combo_box['values'] = data


master1 = Tk()
i = Run(master1)
master1.mainloop()

Hope this helps

  • Related