【发布时间】:2014-02-04 09:38:28
【问题描述】:
我已将回调函数连接到小部件的realize 事件。
小部件是一个树形视图,当它被实现时,它将从数据库中填充表格。 当我从表中打开一个条目时,它会打开一个窗口,我可以在其中编辑数据。 然后我保存数据,窗口关闭,表格可能会刷新。
所以我调用treeview.emit('realize') 再次调用填充树视图的函数。
但是树视图已损坏,没有列标题并且没有调用回调函数。
我也试过了:
treeview.unrealize()
treeview.realize()
一些代码:
from gi.repository import Gtk
b = Gtk.Builder()
b.add_from_file('file.glade')
def fill_table(treeview, table_name):
'fills the treeview from database (select * from table_name ...)'
...
def open_entry(treeview, row ...):
'opens the window of the entry'
w = b.get_object('entry_window')
...
def save_and_fill(button):
'when the SAVE button in the entry_window is clicked, it saves the data, closes the entry_window and fills the treeview'
#update table1 set column=....
b.get_object('treeview').emit('realize') #here it might call the fill_table funcion
b.get_object('entry_window').hide()
signals = {'realize' : lambda *args: fill_table('table1'),
'row-activated' : open_entry,
'clicked' : save_and_fill}
b.connect_signals(signals)
b.get_object('main_window').show_all()
Gtk.main()
或者只是treeview.realize(),但它不起作用。
是否可以调用回调函数witch连接到widget的realize事件而不破坏widget?
【问题讨论】:
-
您能否添加更多代码,这不足以诊断您的问题。
-
首先,您似乎出于自己的目的劫持了实现信号...如果您想从按钮按下处理程序调用 fill_table(),然后从那里调用 fill_table():不要t 发出与另一个对象完全无关的信号。其次,treeview 的工作方式是它应该在模型中的数据(如 ListStore)发生变化时自动更新:应该没有理由从按钮按下处理程序调用任何触及 treeview 的函数:您应该只修改数据模型。
-
我编辑了上面的代码。如果连接到
realize事件的函数是在连接时动态创建的怎么办?我想获取该函数并调用它或发出realize事件来调用该函数。 -
你真的没有业务发出实现信号。如果需要,您可以在实现处理程序中调用 fill_table()(但就像 ebassi 所说,空闲处理程序会更好),并且您可以从单击的处理程序调用 fill_table()。
标签: python callback event-handling gtk