我需要创建一个 GUI 来将数据输入到矩阵或表格中并读取此表单数据.完美的解决方案是限制输入表单只允许float.例如:
I need to create a GUI to input data into a matrix or table and read this form data. The perfect solution is to restrict the input form to allow only float. For example:
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 没有表,Python 3.3.2 不支持 wxPython
What can I use? Tkinter does not have tables and wxPython is not supported in Python 3.3.2
PyQt4 怎么样?也许您可以举例说明如何从 [[],[],[]] 中的表中获取数据?有人有想法吗?
How about PyQt4? Maybe you can give some example on how take data from table in [[],[],[]]? Anyone has ideas?
谢谢
推荐答案使用 tkinter,您不需要特殊的表格小部件来执行此操作——只需创建一个普通条目小部件的网格即可.如果您有太多的滚动条需要滚动条,那会稍微困难一些(这个站点上有一些示例说明如何做到这一点),但只要创建一个由一些小东西组成的网格就非常简单了.
Using tkinter, you don't need a special table widget to do this -- just create a grid of normal entry widgets. If you have so many that you need a scrollbar it's slightly more difficult (and there are examples on this site for how to do that), but just to create a grid of something small it's very straightforward.
这是一个还包含一些输入验证的示例:
Here's an example that also includes some input validation:
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()可以在此处找到有关输入验证的更多信息:以交互方式验证输入小部件内容tkinter
More about input validation can be found here: Interactively validating Entry widget content in tkinter
更多推荐
GUI(输入和输出矩阵)?
发布评论