当Canonical-Quick设置一个新项目时,它的"关于对话框"有以下行:
self.AboutDialog = AboutNewAppDialog
我在 glade 中编辑了菜单项,并将以下代码添加到主窗口的 python 代码中:
self.menuabout = self.builder.get_object("menuabout")
和
def on_menuabout_activate(self, menuitem, data=None):
print("About activated")
self.response = self.AboutDialog.run()
self.AboutDialog.hide()
但这会产生错误:
self.response = self.AboutDialog.run()
TypeError: run() takes exactly 1 argument (0 given)
我也在学习本教程,它使用类似的语法:http://gnipsel.com/glade/glade02b.html
当我将Gtk.Dialog放入括号中时,程序崩溃:
self.response = self.AboutDialog.run(Gtk.Dialog)
我的第二次尝试:
#!/usr/bin/env python
from gi.repository import Gtk
class Handler:
def on_mainwindow_destroy(self, menuitem):
print("destroy window")
Gtk.main_quit()
def on_menuquit_activate(self, menuitem):
print("quit from menu")
Gtk.main_quit()
def on_menuabout_activate(self, menuitem, data=None):
print("menu about activated")
response = aboutdialog.run()
aboutdialog.hide()
builder = Gtk.Builder()
builder.add_from_file("psn.glade")
builder.connect_signals(Handler())
window = builder.get_object("mainwindow")
window.show_all()
Gtk.main()
错误:
"回溯(最近一次调用):文件"psn_main.py",第 21 行,on_menuabout_activate 响应 = 关于对话框.运行()名称错误: 未定义名称'关于对话框' "
我使用以下代码让它工作。该功能由调用"on_menuabout_activate"的菜单项激活。它将调试消息打印到控制台。然后它从 glade 文件中获取 aboutdialog-window 并运行它:
def on_menuabout_activate(self, menuitem, data=None):
print("menu about activated")
aboutdialog = builder.get_object("aboutdialog")
aboutdialog.run()