如何在循环中获取 Python TkInter 多个组合框中的选定项?

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

我想创建一个 table 类似于 Excel 的样式。它应该取决于要显示的数组的大小。 因此,table 列和行在循环周期内。 在第一行中,我将有一个组合框 select 要过滤的值。 此外,组合框在不同列号的循环周期内。

创建 table 时,我无法识别选择了哪个组合框。

我该怎么办?

示例:

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)

终于有时间 post 找到了解决方案,感谢您的帮助:

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)

享受罗伯托