PyGTK + Glade:没有任何信号的按钮触发另一个信号
PyGTK + Glade: button without any signal triggers another signal
我正在使用 PyGTK+ 3.0 和 Glade 3.22.2(和 Python 3.8.1)开发一个 Python 应用程序。一切都很好,但突然间,我的程序开始出现奇怪的行为。我有两种方法来处理一些按钮点击:on_save_file()
和 on_open_file()
。这些函数将 Gtk.Entry
作为参数和 open/save 文件并将文件名设置为提供的 Gtk.Entry
.
奇怪的部分现在开始了:突然,一个打开文件的按钮开始神奇地触发对另一个 windows 中保存文件的其他两个按钮的点击。结果是三个文件对话框同时出现。
为了测试,我删除了那个按钮的 on_open_file
信号。并继续触发其他两个...
我也尝试取回一些提交,但错误仍然存在(我几乎可以肯定它以前没有发生过,就像我几乎可以肯定我没有更新任何东西一样我的虚拟环境)。
有人知道这个问题可能出在哪里吗?
谢谢!
更多详情
这是我的三种方法(和信号处理程序):
# class blahblahblah: ...
def open_file(self, use_last_path=True) -> str:
"""
Show an Open File dialog and return the filename
:param use_last_path: Whether or not to start at the last folder opened
:return: the selected filename
"""
file_chooser = Gtk.FileChooserDialog(
title="Open...", action=Gtk.FileChooserAction.OPEN)
file_chooser.add_buttons(
"Cancel", Gtk.ResponseType.CANCEL, "Open", Gtk.ResponseType.OK)
file_chooser.set_default_response(Gtk.ResponseType.OK)
file_chooser.add_filter(self.csv_file_filter)
if self.last_opened_path and use_last_path:
file_chooser.set_current_folder(self.last_opened_path)
response = file_chooser.run()
filename = None
if response == Gtk.ResponseType.OK:
filename = file_chooser.get_filename()
self.last_opened_path = os.path.dirname(os.path.abspath(filename))
file_chooser.destroy()
return filename
def on_save_file(self, entry: Gtk.Entry) -> None:
"""Show a Save File dialog and set the filename to the given entry"""
file_chooser = Gtk.FileChooserDialog(
title="Save...", action=Gtk.FileChooserAction.SAVE)
file_chooser.add_buttons(
"Cancel", Gtk.ResponseType.CANCEL, "Save", Gtk.ResponseType.OK)
file_chooser.set_default_response(Gtk.ResponseType.OK)
file_chooser.add_filter(self.csv_file_filter)
response = file_chooser.run()
if response == Gtk.ResponseType.OK:
filename = file_chooser.get_filename()
entry.set_text(filename)
file_chooser.destroy()
def on_open_file(self, entry: Gtk.Entry) -> None:
"""Show an Open File dialog and set the filename to the given entry"""
filename = self.open_file()
if filename:
entry.set_text(filename)
应该打开文件的按钮(正如我所说,我删除了 on_open_file
信号)。
<object class="GtkButton">
<property name="label" translatable="yes">Open</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
触发的按钮:
<object class="GtkButton">
<property name="label" translatable="yes">Browse</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_save_file" object="GA_output_matrix" swapped="no"/>
</object>
<object class="GtkButton">
<property name="label" translatable="yes">Browse</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_save_file" object="OPS_output_matrix" swapped="no"/>
</object>
上面的每个按钮都在不同的文件中,我使用 Gtk.Builder
:
加载它们
builder: Gtk.Builder = Gtk.Builder()
builder.add_from_file('./Views/main.glade')
builder.add_from_file('./Views/ga.glade')
builder.add_from_file('./Views/ops.glade')
if __name__ == '__main__':
builder.connect_signals(Handler())
window = builder.get_object('main_window')
window.show_all()
Gtk.main()
我不知道为什么,但是为每个按钮添加一个 ID 解决了这个错误。按钮变为:
<object class="GtkButton" id="a_semantic_id_1">
<property name="label" translatable="yes">Open</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<object class="GtkButton" id="a_semantic_id_2">
<property name="label" translatable="yes">Browse</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_save_file" object="GA_output_matrix" swapped="no"/>
</object>
<object class="GtkButton" id="a_semantic_id_3">
<property name="label" translatable="yes">Browse</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_save_file" object="OPS_output_matrix" swapped="no"/>
</object>
我正在使用 PyGTK+ 3.0 和 Glade 3.22.2(和 Python 3.8.1)开发一个 Python 应用程序。一切都很好,但突然间,我的程序开始出现奇怪的行为。我有两种方法来处理一些按钮点击:on_save_file()
和 on_open_file()
。这些函数将 Gtk.Entry
作为参数和 open/save 文件并将文件名设置为提供的 Gtk.Entry
.
奇怪的部分现在开始了:突然,一个打开文件的按钮开始神奇地触发对另一个 windows 中保存文件的其他两个按钮的点击。结果是三个文件对话框同时出现。
为了测试,我删除了那个按钮的 on_open_file
信号。并继续触发其他两个...
我也尝试取回一些提交,但错误仍然存在(我几乎可以肯定它以前没有发生过,就像我几乎可以肯定我没有更新任何东西一样我的虚拟环境)。
有人知道这个问题可能出在哪里吗?
谢谢!
更多详情
这是我的三种方法(和信号处理程序):
# class blahblahblah: ...
def open_file(self, use_last_path=True) -> str:
"""
Show an Open File dialog and return the filename
:param use_last_path: Whether or not to start at the last folder opened
:return: the selected filename
"""
file_chooser = Gtk.FileChooserDialog(
title="Open...", action=Gtk.FileChooserAction.OPEN)
file_chooser.add_buttons(
"Cancel", Gtk.ResponseType.CANCEL, "Open", Gtk.ResponseType.OK)
file_chooser.set_default_response(Gtk.ResponseType.OK)
file_chooser.add_filter(self.csv_file_filter)
if self.last_opened_path and use_last_path:
file_chooser.set_current_folder(self.last_opened_path)
response = file_chooser.run()
filename = None
if response == Gtk.ResponseType.OK:
filename = file_chooser.get_filename()
self.last_opened_path = os.path.dirname(os.path.abspath(filename))
file_chooser.destroy()
return filename
def on_save_file(self, entry: Gtk.Entry) -> None:
"""Show a Save File dialog and set the filename to the given entry"""
file_chooser = Gtk.FileChooserDialog(
title="Save...", action=Gtk.FileChooserAction.SAVE)
file_chooser.add_buttons(
"Cancel", Gtk.ResponseType.CANCEL, "Save", Gtk.ResponseType.OK)
file_chooser.set_default_response(Gtk.ResponseType.OK)
file_chooser.add_filter(self.csv_file_filter)
response = file_chooser.run()
if response == Gtk.ResponseType.OK:
filename = file_chooser.get_filename()
entry.set_text(filename)
file_chooser.destroy()
def on_open_file(self, entry: Gtk.Entry) -> None:
"""Show an Open File dialog and set the filename to the given entry"""
filename = self.open_file()
if filename:
entry.set_text(filename)
应该打开文件的按钮(正如我所说,我删除了 on_open_file
信号)。
<object class="GtkButton">
<property name="label" translatable="yes">Open</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
触发的按钮:
<object class="GtkButton">
<property name="label" translatable="yes">Browse</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_save_file" object="GA_output_matrix" swapped="no"/>
</object>
<object class="GtkButton">
<property name="label" translatable="yes">Browse</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_save_file" object="OPS_output_matrix" swapped="no"/>
</object>
上面的每个按钮都在不同的文件中,我使用 Gtk.Builder
:
builder: Gtk.Builder = Gtk.Builder()
builder.add_from_file('./Views/main.glade')
builder.add_from_file('./Views/ga.glade')
builder.add_from_file('./Views/ops.glade')
if __name__ == '__main__':
builder.connect_signals(Handler())
window = builder.get_object('main_window')
window.show_all()
Gtk.main()
我不知道为什么,但是为每个按钮添加一个 ID 解决了这个错误。按钮变为:
<object class="GtkButton" id="a_semantic_id_1">
<property name="label" translatable="yes">Open</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<object class="GtkButton" id="a_semantic_id_2">
<property name="label" translatable="yes">Browse</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_save_file" object="GA_output_matrix" swapped="no"/>
</object>
<object class="GtkButton" id="a_semantic_id_3">
<property name="label" translatable="yes">Browse</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_save_file" object="OPS_output_matrix" swapped="no"/>
</object>