2013-09-24 220 views
-2

Python 3.3.2 Hello.My問題 - 需要爲輸入數據(矩陣或表格)創建GUI,並從這個表單數據中獲取數據。例如:GUI(輸入和輸出矩陣)?

A=[[1.02,-0.25,-0.30,0.515],[-0.41,1.13,-0.15,1.555],[-0.25,-0.14,1.21,2.780]]

完美的解決方案是限制到輸入形式(僅浮動)。

問題: 我能用什麼? Tkinter有沒有表.. wxPython不支持Python 3. PyQt4?(MB你有exampel如何從數據從表中的數據在[[],[],[]]?)
任何人有想法嗎?

回答

6

使用tkinter,你不需要一個特殊的表格小部件來做到這一點 - 只需創建一個普通的入口小部件的網格。如果你有這麼多,你需要一個滾動條,它會稍微困難一點(在這個網站上有一些例子來說明如何做),但是創建一個小的網格非常簡單。

下面是一個例子,其中還包括一些輸入驗證:

import tkinter as tk 

class SimpleTableInput(tk.Frame): 
    def __init__(self, parent, rows, columns): 
     tk.Frame.__init__(self, parent) 

     self._entry = {} 
     self.rows = rows 
     self.columns = columns 

     # register a command to use for validation 
     vcmd = (self.register(self._validate), "%P") 

     # create the table of widgets 
     for row in range(self.rows): 
      for column in range(self.columns): 
       index = (row, column) 
       e = tk.Entry(self, validate="key", validatecommand=vcmd) 
       e.grid(row=row, column=column, stick="nsew") 
       self._entry[index] = e 
     # adjust column weights so they all expand equally 
     for column in range(self.columns): 
      self.grid_columnconfigure(column, weight=1) 
     # designate a final, empty row to fill up any extra space 
     self.grid_rowconfigure(rows, weight=1) 

    def get(self): 
     '''Return a list of lists, containing the data in the table''' 
     result = [] 
     for row in range(self.rows): 
      current_row = [] 
      for column in range(self.columns): 
       index = (row, column) 
       current_row.append(self._entry[index].get()) 
      result.append(current_row) 
     return result 

    def _validate(self, P): 
     '''Perform input validation. 

     Allow only an empty value, or a value that can be converted to a float 
     ''' 
     if P.strip() == "": 
      return True 

     try: 
      f = float(P) 
     except ValueError: 
      self.bell() 
      return False 
     return True 

class Example(tk.Frame): 
    def __init__(self, parent): 
     tk.Frame.__init__(self, parent) 
     self.table = SimpleTableInput(self, 3, 4) 
     self.submit = tk.Button(self, text="Submit", command=self.on_submit) 
     self.table.pack(side="top", fill="both", expand=True) 
     self.submit.pack(side="bottom") 

    def on_submit(self): 
     print(self.table.get()) 


root = tk.Tk() 
Example(root).pack(side="top", fill="both", expand=True) 
root.mainloop() 

更多的輸入驗證可以在這裏找到:Interactively validating Entry widget content in tkinter