Home > Enterprise >  Tkinter python 3.8 comboboxes in loop. How correct wrong choice while appending to list?
Tkinter python 3.8 comboboxes in loop. How correct wrong choice while appending to list?

Time:11-15

I´ve created a loop of comboboxes and I'm appending the choices I make to a list. There are 3 comboboxes. The list must have 3 corresponding elements. The problem is that if I change my mind while choosing and replace the option, the list (of course) increases in size. In this code I have 3 options: 'The One!', 'more or less','the bad'. I want to change the options during selection, but keeping the list always with 3 unique elements corresponding to number of combobox. I've tought about using set but didn't work because the actual loop is large.

I need the list with just the len of comboboxes with result corresponding to the last choices, independently of the number of choice changes I've done.

Thank you for any help.

from tkinter import  ttk
from tkinter import Tk
from tkinter import Button

root = Tk()
my_heroes = ['Zidane', 'Ronaldo', 'Messi']
position = ['The One!', 'more or less','the bad']
result =[]

def get_combo_choice(event, cmb):
    result.append(cmb.get())
    print(result)
    
  
for index, heroe in enumerate(my_heroes):
    var = StringVar()
    bestPlayers = ttk.Combobox(root,values=position, textvariable=var, state="readonly")
    bestPlayers.grid(row=0   index, column=1,padx=(15,25))
    
    label = Label(root, text = heroe)
    label.grid(row=0   index, column=0,padx=(15,25))

    bestPlayers.bind("<<ComboboxSelected>>",lambda event, cmb=var:get_combo_choice(event, cmb))

    
    button = Button(root, text ="get list of choices", command = callback)
    button.grid(row=4, column=0,padx=(15,25))

root.mainloop()

The print grows to three, ok, but then continues... I need to keep size of list = combobox len and freely update the values getting the last 3 choices at the end.

['The One!']

['The One!', 'more or less']

['The One!', 'more or less', 'the bad']

['The One!', 'more or less', 'the bad', 'The One!']

['The One!', 'more or less', 'the bad', 'The One!', 'more or less']

['The One!', 'more or less', 'the bad', 'The One!', 'more or less', 'The One!']

['The One!', 'more or less', 'the bad', 'The One!', 'more or less', 'The One!', 'more or less']

['The One!', 'more or less', 'the bad', 'The One!', 'more or less', 'The One!', 'more or less', 'more or less']

CodePudding user response:

You can limit list length by usng list slice.

def get_combo_choice(event, cmb):
    global result
    result.append(cmb.get())
    # limit list len to 3 using ~2
    result = result[~2:]
    print(result)

In order to control result columns a few things need to be added to your code.

result =[None, None, None]
best = []

# Here's the alternative 
def get_combo_choice(event, cmb):
    i = best.index(event.widget)
    result[i] = cmb.get()
    print(result)

Inside the for\loop insert best.append(bestPlayers)

CodePudding user response:

Mr Derek I updated my code with your answer. It works like a charm!! You teached me a lot, big thanks!

import tkinter as tk
from tkinter import  ttk
from tkinter import Tk
from tkinter import Button

root = Tk()
my_heroes = ['Zidane', 'Ronaldo', 'Messi']
position = ['The One!', 'more or less','the bad']
# result =[]

# def get_combo_choice(event, cmb):
#     result.append(cmb.get())
#     print(result)
result =[None, None, None]
best = []

# Here's the alternative 
def get_combo_choice(event, cmb):
    i = best.index(event.widget)
    result[i] = cmb.get()
    print(result)    
  
for index, heroe in enumerate(my_heroes):
    var = tk.StringVar()
    bestPlayers = ttk.Combobox(root,values=position, textvariable=var, state="readonly")
    best.append(bestPlayers)
    bestPlayers.grid(row=0   index, column=1,padx=(15,25))
    
    label = tk.Label(root, text = heroe)
    label.grid(row=0   index, column=0,padx=(15,25))

    bestPlayers.bind("<<ComboboxSelected>>",lambda event, cmb=var:get_combo_choice(event, cmb))

    
    button = tk.Button(root, text ="get list of choices", command = get_combo_choice)
    button.grid(row=4, column=0,padx=(15,25))

root.mainloop()

code results in :

['The One!', None, None]

['The One!', None, None]

['The One!', None, None]

['The One!', 'more or less', None]

['The One!', 'more or less', 'the bad']

CodePudding user response:

One last question: in: def get_combo_choice(event, cmb):

I understand event(choice--> ex:"the one"),

cbm (--> the variable) and that results uses the index to get choice from best(list),

what I dont understand is why I can't use i = best.index() if best is a list we already know. What is (event.widget) and what it is doing there? By the way your code fits all my needs.

  • Related