简体   繁体   中英

How to get the selected item in a Python TkInter multiple combobox in a loop?

I would like to create a table similar to Excel's style. It should be depending on the size of the array to show. For this reason the table columns and rows are inside loop cycles. In the first line I would have a combobox to select the value to filter. Also the combobox is inside a loop cycle for the different columns number.

When the table is created I am not able to recognize which combobox is chosen.

How can I do?

截屏

Example:

import tkinter
from tkinter import ttk #per button, label etc
from tkinter.ttk import *
import numpy as np #for the matrix tools

def newselection(event, output):
    print("Value selected:", event, output)

def ShowTableGui(MatrixToShowIn):
    MatrixToShow=np.array(MatrixToShowIn)
    RowNumber = MatrixToShow.shape[0]
    ArrayCombo=[]
    windowx=tkinter.Tk()
    windowx.title("Table viewer")
    buttonExit = ttk.Button(windowx, text="Close table", command=windowx.destroy)
#    buttonExit = ttk.Button(windowx, text="Run Filter", command= lambda: Run_filter(MatrixToShow.shape[1]))
    buttonExit.grid(column=0, row=0)
    for Col in range (int(MatrixToShow.shape[1])):
        ValuesInsert=MatrixToShow[:,Col] # values in column
        ValuesInsert=[row[Col]for row in MatrixToShowIn]
        ValuesInsert=list(set(ValuesInsert)) # values listed only once
        ValuesInsert.sort()
        ValuesInsert.insert(0,"*") # add * to filter all
        comboExample0 = ttk.Combobox(windowx, state='readonly', values=ValuesInsert)
        comboExample0.grid(column=Col+2, row=0)
        comboExample0.bind("<<ComboboxSelected>>", lambda event:newselection(comboExample0.get(), "output"))
#        comboExample0.bind("<<ComboboxSelected>>", lambda event:newselection(event, "output"))
        ArrayCombo.append(comboExample0)
        Select=comboExample0.get()
        print(Select)
        for Row in range (RowNumber):
            b = Entry(windowx, text="")
            b.grid(row=Row+1, column=Col+2)
            b.insert(0,str(MatrixToShow[Row][Col]))
    windowx.mainloop()
    return()
    
MatrixToShowIn=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
ShowTableGui(MatrixToShowIn)

Finally I have the time to post the solution found thanks to your help:

from tkinter import *
from functools import partial
from tkinter.ttk import *

class ComboTest:
   def __init__(self, MatrixToShow):
      self.top = Tk()
      self.top.title('Combos Test')
      self.top_frame = Frame(self.top, width =400, height=400)
      self.button_dic = {}
      self.combos_dic = {}
      self.var = StringVar()
      self.top_frame.grid(row=0, column=1)
      Button(self.top_frame, text='Exit', 
              command=self.top.destroy).grid(row=0,column=0, columnspan=5)
      self.combos(MatrixToShow)
      self.top.mainloop()

   ##-------------------------------------------------------------------         
   def combos(self,MatrixToShow):
      b_row=1
      Columns=[]
      for com_num in range(len(MatrixToShow[0])):
         Column=["*"]
         for Row in range(len(MatrixToShow)):
              Column.append(MatrixToShow[Row][com_num])
         Columns.append(Column)
         ## note that the correct "com_num" is stored
#         self.combos_dic[com_num] = "self.cb_combo_%d()" % (com_num)
         e = Entry(self.top_frame)
         e.insert(0, com_num)
         e.insert(0, "Column")
         e.grid(row=b_row, column=com_num)
         b = Combobox(self.top_frame, state='readonly', values=Column)
         b.bind("<<ComboboxSelected>>", partial(self.cb_handler, com_num))
         b.current(0)
         b.grid(row=b_row+1, column=com_num)

   ##----------------------------------------------------------------
   def cb_handler( self, cb_number, event ):
      print ("ComboNumber", cb_number, "SelectedValue",  event.widget.get())                
          

##=================================================================
MatrixToShowIn=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
CT=ComboTest(MatrixToShowIn)

Enjoy Roberto

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM