我正在使用 tkintertable 并想检查选择了哪一行。这可能吗?而且我还想对列进行排序并按我想要的方式命名列...
我怎样才能做到这一点?
我有点晚了,但我想我会回答这个问题,以防其他人像我一样通过谷歌搜索找到这个:P
有几种方法可以根据鼠标点击来选择数据。
首先你需要将鼠标点击绑定到一个回调函数,在这个回调函数中你需要抓取数据。您可能需要在释放时绑定鼠标单击,以便在释放鼠标按钮时抓取数据。
您可以使用 table.get_row_clicked()/table.get_col_clicked() 函数和 model.getValueAt() 来获取单个单元格。
要获取字典中整行的内容,其中 column_name = key & 所选行的内容 column = value,您需要使用 model.getRecordAtRow(row_index)。
例子:
from Tkinter import *
from ttk import *
from tkintertable.Tables import TableCanvas
from tkintertable.TableModels import TableModel
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.model = TableModel()
self.table = TableCanvas(self, model=self.model)
self.table.createTableFrame()
root.bind('<ButtonRelease-1>', self.clicked) #Bind the click release event
self.create_widgets()
def create_widgets(self):
self.table.model.load('save.table') #You don't have to load a model, but I usually
self.table.redrawTable() #Create a base model for my tables.
d = dir(self.table) #Will show you what you can do with tables. add .model
#to the end to see what you can do with the models.
for i in d:
print i
def clicked(self, event): #Click event callback function.
#Probably needs better exception handling, but w/e.
try:
rclicked = self.table.get_row_clicked(event)
cclicked = self.table.get_col_clicked(event)
clicks = (rclicked, cclicked)
print 'clicks:', clicks
except:
print 'Error'
if clicks:
#Now we try to get the value of the row+col that was clicked.
try: print 'single cell:', self.table.model.getValueAt(clicks[0], clicks[1])
except: print 'No record at:', clicks
#This is how you can get the entire contents of a row.
try: print 'entire record:', self.table.model.getRecordAtRow(clicks[0])
except: print 'No record at:', clicks
root = Tk()
root.title('Table Test')
app = Application(master=root)
print 'Starting mainloop()'
app.mainloop()
至于其他事情,您应该能够从 wiki 获取信息:
http://code.google.com/p/tkintertable/wiki/Usage
有些事情很难弄清楚,但IMO值得。我仍然不知道如何以编程方式更改单元格的内容,这在 wiki 页面上是正确的,哈哈。