簡體   English   中英

如何循環獲取一個Python TkInter多個combobox中的選中項?

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

我想創建一個類似於 Excel 樣式的表格。 它應該取決於要顯示的數組的大小。 由於這個原因,表列和行都在循環周期內。 在第一行中,我將使用 combobox 到 select 的值進行過濾。 combobox 也在不同列號的循環中。

創建表時,我無法識別選擇了哪個 combobox。

我能怎么做?

截屏

例子:

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)

最后我有時間發布在您的幫助下找到的解決方案:

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)

享受羅伯托

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM