-1
root = tk.Tk()
root.title("Root")
root.configure(bg = "#333333")   
root.geometry("600x500")          
root.resizable(False, False) 

def Category(category):
    y = 40
    category_list = [['Refrigerator', 'Refrigerator', 'Refrigerator', 'Refrigerator', 'Refrigerator', 'Television', 'Television', 'Television', 'Television', 'Television'], ['GE', 'LG', 'Maytag', 'Samsung', 'Whirlpool', 'Hisense', 'LG', 'Samsung', 'Sony', 'TCL']]
    category_item = category_list[0]
    brand = category_list[1]
    for x in range(len(category_item)):
        if category_item[x] == category:
            if category == "Television":
                brand_button = Button(frame, text = brand[x], font = "Poppins, 10", bg = "#222222", fg = "#ffffff", width = 12, borderwidth = 0, highlightthickness = 0, activebackground = "#222222", activeforeground = "#ffffff", anchor = "w")
                brand_button.bind("<Button-1>", lambda e:getTV(brand[x]))
                brand_button.place(x = 40, y = y)
                y += 35
            else:
                brand_button = Button(frame, text = brand[x], font = "Poppins, 10", bg = "#222222", fg = "#ffffff", width = 12, borderwidth = 0, highlightthickness = 0, activebackground = "#222222", activeforeground = "#ffffff", anchor = "w")
                brand_button.bind("<Button-1>", lambda e:getFridge(brand[x]))
                brand_button.place(x = 40, y = y)
                y += 35

def getFridge(user_input):
    record = ['LG', 'GE', 'Maytag', 'Samsung', 'Whirlpool']
    for row in record:
        if user_input == row:
            print("This is", row)

def getTV(user_input):
    record = ['Hisense', 'LG', 'Samsung', 'Sony', 'TCL']
    for row in record:
        if user_input == row:
            print("This is", row)

Category("Refrigerator")
root.mainloop()

So, I'm creating a tk.Button using a for loop. And for the name of the button are the same, and all of them are able to do the command. However, the argument(brand[x]) that is given to the function are all the same which is (TCL).

The photo of the button
This is the result that I get, no matter which button that I clicked

What I want is the output will be the same as the name of the brand name. For example, when I clicked GE button, it will give output 'This is GE'

def getFridge(user_input**[GE]**):
    record = ['LG', 'GE', 'Maytag', 'Samsung', 'Whirlpool']
    for row in record:
        if user_input**[GE]** == row**[GE]**:
            print("This is", row**[GE]**)

2 Answers2

0

I have personally tried this before on a tkinter calculator where I tried to use a for loop to create the buttons, but same as you, this didn't work and no matter what button I clicked, the output was the number 9.

I almost posted a question, but after some brainstorming, I figured out that you just can't do that with a for loop and keep it simple because the for loop creates the buttons fine but at the end, its done and all that gets passed into the lambda function is the last item in the for loop (which is why the argument(brand[x]) that is given to the function are all the same which is (TCL). and why it was 9 for me.)

I don't know about you, but for me this was so annoying and I then resolved that I would have to take the pain and sweat and hard effort to create these buttons one-by-one.

Maybe there's some other way out there and i'm sure someone will post it, but you can be sure that if you create the buttons one-by-one, you can be sure of what gets passed in.

Have a nice day :-)

  • Hello there, thank you for the info. But actually I've just try another way from the link that Jasonharper sent. And I found out that it works using the ```functools.partial``` and for mine it's ```command = partial(self.getFridge, brand[x])``` – Ferdinand Jacques Dec 15 '22 at 16:14
0

thank you guys for the info. But actually I've just try another way from the link that Jasonharper sent. And I found out that it works using the functools.partial and for mine it's command = partial(self.getFridge, brand[x])