#!/usr/bin/python
# -*- coding: ISO-8859-1 -*-
from gi.repository import GLib, Gtk, Gio
import sys
from gi.repository import Gdk
from gi.repository.GdkPixbuf import Pixbuf, InterpType
class MyAbout(Gtk.AboutDialog):
__gtype_name__ = 'MyAbout' # nom pour css
def __init__(self,title='',version="1.0",\
authors=["Francis"],\
name="PyGObject Tutorial",\
comment="New tutorial on using Python with GTK+ 3",\
website="http://www.learngtk.org/",\
label="LearnGTK Website",\
filename=None,\
size_x=100, size_y=100):
Gtk.AboutDialog.__init__(self)
self.set_title(title)
self.set_version(version)
self.set_authors(authors)
if filename is not None:
file = filename
else:
file = "/home/francis/Documents/Python-linux/demo_gtk2/battery.png"
pixbuf = Pixbuf.new_from_file_at_size(file,size_x, size_y)
self.set_logo(pixbuf)
self.set_name(name)
self.set_comments(comment)
self.set_website(website)
self.set_website_label(label)
__version__ = "V1_30"
class MyApp_root(object):
""" just a little app with few menu """
def __init__(self):
GLib.set_application_name('My App')
self.app = Gtk.Application.new('org.example.test', 0)
self.app.connect('startup', self.on_app_startup)
self.app.connect('activate', self.on_app_activate)
self.app.connect('shutdown', self.on_app_shutdown)
def add_sub_menu(self,texte, menu):
""" just add submenu to menu return need reference to sub_menu"""
submenu = Gio.Menu()
menu.append_submenu(texte, submenu)
return submenu
def add_menu_bar(self,app):
""" just add function menubar app need refernce to app_menu"""
menu_bar = Gio.Menu()
app.set_menubar(menu_bar)
return menu_bar
def add_section(self,app):
""" just add a section
note between 2 sections there is a separator"""
section = Gio.Menu()
app.append_section(None, section)
return section
def add_simple_action(self, app, name, callback):
""" in order to create callback fonction linked menu"""
action = Gio.SimpleAction.new(name, None)
action.connect('activate', callback)
app.add_action(action)
def add_menuitem(self,section,text,link,attribut_type=None, attribut_value=None):
""" just add a item with parameter """
item = Gio.MenuItem.new(text,link)
if attribut_type is not None:
# example item.set_attribute_value("accel", GLib.Variant("s", "<Control>Q"))
item.set_attribute_value(attribut_type, attribut_value)
section.append_item(item)
def run(self, argv):
""" in order to run class """
self.app.run(argv)
def on_app_startup(self, app):
""" auto start app in window, set menu and call back
standard design in python callback action is action_cb"""
self.window = Gtk.ApplicationWindow.new(app)
self.window.set_default_size(640, 480)
self.window.set_title('AppMenu Demo')
app.add_window(self.window)
# # add Appmenu to app
self.app_menu = Gio.Menu()
section = self.add_section(self.app_menu)
self.add_menuitem(section,'Quit', 'app.quit',"accel", GLib.Variant("s", "<Control>Q"))
# it's necessary to link app-menu to app after section definition if not accelerator don't operate
app.set_app_menu(self.app_menu)
self.add_simple_action(app,'quit',self.quit_cb)
self.add_simple_action(app,'histo',self.histo_cb)
self.add_simple_action(app,'about',self.about_cb)
self.add_simple_action(app,'help',self.help_cb)
# # Menu bar attached app
self.menu_bar = self.add_menu_bar(app)
#---création submenu1
self.submenu1 = self.add_sub_menu('Miscellaneous',self.menu_bar)
#---création de section 3
section3 = self.add_section(self.submenu1)
#---gestion des item
self.add_menuitem(section3,'Help', 'app.help',"accel", GLib.Variant("s", "<Control>H"))
self.add_menuitem(section3,'About', 'app.about')
self.add_menuitem(section3,'Historic version', 'app.histo')
#code writed here in order to debug
submenu1 = self.add_sub_menu('Miscellaneous 1',self.menu_bar)
# # add section to submenu
section1 = self.add_section(submenu1)
# add item to section
self.add_simple_action(app,'pref',self.pref_cb)
self.add_menuitem(section1,'Preference', 'app.pref',"accel", GLib.Variant("s", "<Control>P"))
def on_app_activate(self, app):
self.window.show_all()
def on_app_shutdown(self, app):
pass
#here only in order to debug corresponding part and deleted after
def pref_cb(self, action, data=None):
print 'action preference not dev !!!!!!!!'
# end block to be deleted
def histo_cb(self, action, data=None):
print 'historique des versions'
print 'Version 1.30 add root class with basic menu '
print '#Version 1.31 > a class inherit MyApp_root and try to add something'
def about_cb(self, action, data=None):
about = MyAbout(version=__version__)
about.run()
def help_cb(self, action, data=None):
print "aide non developpe"
def quit_cb(self, action, data=None):
self.app.quit()
class MyAppNew(MyApp_root):
""" inherit class MyApp_root """
def __init__(self):
MyApp_root.__init__(self);
# # add submenu attached menu_bar
submenu = self.add_sub_menu('Miscellaneous next',self.menubar) #error here AttributeError: 'MyAppNew' object has no attribute 'menubar'
section1 = self.add_section(submenu)
# add item to section
self.add_menuitem(section1,'work hard', 'app.work')
self.add_simple_action(app,'work',self.work_cb)
def run(self, argv):
""" in order to run class """
pass # ??????
# here start to transfer all call back not necessary in root class
def work_cb(self, action, data=None):
print " It's very hard for me to make a class inherit class MyApp_root"
# to adapt a new help
def help_cb(self, action, data=None):
print "How to implement polymorhisme in order to change action in th child class here ??"
if __name__ == '__main__':
new_test = True
if new_test:
app = MyAppNew()
else:
application = MyApp_root()
application.run(sys.argv)
各位大家好, 我知道为什么会出现错误,self.menubar不是在根MyApp_root的init中创建的。但是当事件“启动”发生时,它是用self.on_app_startup创建的。 在开始上课时。好的,据说 但现在谁能帮助我并告诉我如何在MyAppNew中添加一些新的子菜单。非常感谢提前
答案 0 :(得分:1)
在这种情况下,假设您真的想要像这样工作,解决方案是确保在创建self.menu_bar
之后添加新的子菜单。
在您的情况下,最简单的方法是在self.insert_custom_menu
中添加MyApp_root
之类的函数(或类似内容),您将在MyAppNew
中覆盖该函数。如果您在on_app_startup
中对此功能进行了函数调用,则可以轻松添加新的菜单选项
因此,例如在MyApp_root
中,函数将是这样的:
def insert_custom_menu(self):
pass
在MyAppNew
中,函数将是这样的:
def insert_custom_menu(self):
submenu = self.add_sub_menu('Miscellaneous next',self.menubar)
section1 = self.add_section(submenu)
self.add_menuitem(section1,'work hard', 'app.work')
这样做的原因是MyAppNew
在init
期间不会尝试向菜单添加内容,而菜单仅在startup
信号之后创建。通过上述解决方案,菜单的自定义部分也会在startup
之后加载,从而解决了手头的问题。
答案 1 :(得分:1)
为了帮助。继承类Gtk.Application的示例。它在我的linux上运行,除了顶级菜单上的错误统一“”未知的应用程序名称“”出现而不是真正的名称它是知道的。 这只是一个微薄的贡献
#!/usr/bin/python
# -*- coding: ISO-8859-1 -*-
from gi.repository import GLib, Gtk, Gio
import sys
from gi.repository import Gdk
from gi.repository.GdkPixbuf import Pixbuf, InterpType
class MyAbout(Gtk.AboutDialog):
__gtype_name__ = 'MyAbout' # nom pour css
def __init__(self,title='',version="1.0",\
authors=["Francis"],\
name="PyGObject Tutorial",\
comment="New tutorial on using Python with GTK+ 3",\
website="http://www.learngtk.org/",\
label="LearnGTK Website",\
filename=None,\
size_x=100, size_y=100):
Gtk.AboutDialog.__init__(self)
self.set_title(title)
self.set_version(version)
self.set_authors(authors)
if filename is not None:
file = filename
else:
file = "/home/francis/Documents/Python-linux/demo_gtk3/battery.png"
try:
pixbuf = Pixbuf.new_from_file_at_size(file,size_x, size_y)
self.set_logo(pixbuf)
except:
self.set_name(name)
self.set_comments(comment)
self.set_website(website)
self.set_website_label(label)
__version__ = "V1_30"
class MyApp_root(object):
""" just a little app with few menu """
def __init__(self):
GLib.set_application_name('My App')
self.app = Gtk.Application.new('org.example.test', 0)
self.app.connect('startup', self.on_app_startup)
self.app.connect('activate', self.on_app_activate)
self.app.connect('shutdown', self.on_app_shutdown)
def add_sub_menu(self,texte, menu):
""" just add submenu to menu return need reference to sub_menu"""
submenu = Gio.Menu()
menu.append_submenu(texte, submenu)
return submenu
def add_menu_bar(self,app):
""" just add function menubar app need refernce to app_menu"""
menu_bar = Gio.Menu()
app.set_menubar(menu_bar)
return menu_bar
def add_section(self,app):
""" just add a section
note between 2 sections there is a separator"""
section = Gio.Menu()
app.append_section(None, section)
return section
def add_simple_action(self, app, name, callback):
""" in order to create callback fonction linked menu"""
action = Gio.SimpleAction.new(name, None)
action.connect('activate', callback)
app.add_action(action)
def add_menuitem(self,section,text,link,attribut_type=None, attribut_value=None):
""" just add a item with parameter """
item = Gio.MenuItem.new(text,link)
if attribut_type is not None:
# example item.set_attribute_value("accel", GLib.Variant("s", "<Control>Q"))
item.set_attribute_value(attribut_type, attribut_value)
section.append_item(item)
def run(self, argv):
""" in order to run class """
self.app.run(argv)
def insert_custom_menu(self):
""" in order to add custom menu later"""
pass
def insert_custom_submenu_first(self):
""" in order to add something in the first submenu before Quit """
## current section is self.custom_section
pass
def insert_custom_submenu_last(self):
""" in order to add something in the last submenu before Quit """
## current section is self.custom_section_last
pass
def on_app_startup(self, app):
""" auto start app in window, set menu and call back
standard design in python callback action is action_cb"""
self.window = Gtk.ApplicationWindow.new(app)
self.window.set_default_size(640, 480)
self.window.set_title('AppMenu Demo')
app.add_window(self.window)
# # add Appmenu to app
self.app_menu = Gio.Menu()
# insert something in the first submenu in dedicated section
self.custom_section = self.add_section(self.app_menu)
self.insert_custom_submenu_first()
# last section to quit action
section_last = self.add_section(self.app_menu)
self.add_menuitem(section_last,'Quit', 'app.quit',"accel", GLib.Variant("s", "<Control>Q"))
# it's necessary to link app-menu to app after section definition if not, accelerator don't operate
app.set_app_menu(self.app_menu)
self.add_simple_action(app,'quit',self.quit_cb)
self.add_simple_action(app,'histo',self.histo_cb)
self.add_simple_action(app,'about',self.about_cb)
self.add_simple_action(app,'help',self.help_cb)
# # Menu bar attached app
self.menu_bar = self.add_menu_bar(app)
# planed if custum menu
self.insert_custom_menu()
#---make submenu1
self.submenu1 = self.add_sub_menu('Miscellaneous',self.menu_bar)
#---make section 3
section3 = self.add_section(self.submenu1)
#---item management
self.add_menuitem(section3,'Help', 'app.help',"accel", GLib.Variant("s", "<Control>H"))
self.add_menuitem(section3,'About', 'app.about')
self.add_menuitem(section3,'Historic version', 'app.histo')
## last section dedicated to custom add in last submenu
self.custom_section_last= self.add_section(self.submenu1)
self.insert_custom_submenu_last()
def on_app_activate(self, app):
self.window.show_all()
def on_app_shutdown(self, app):
pass
def histo_cb(self, action, data=None):
print 'historique des versions'
print 'Version 1.30 add root class with basic menu '
print '#Version 1.31 > a class inherit MyApp_root and try to add something'
def about_cb(self, action, data=None):
""" about app"""
about = MyAbout(version=__version__)
about.run()
def help_cb(self, action, data=None):
""" in order to help user """
print "aide non developpe"
def quit_cb(self, action, data=None):
self.app.quit()
class MyAppNew(MyApp_root):
""" inherit class MyApp_root """
def __init__(self):
MyApp_root.__init__(self);
def run(self, argv):
""" in order to run class """
self.app.run(argv)
def insert_custom_submenu_first(self):
""" in order to add something in the first submenu before Quit """
## current section is self.custom_section
self.add_menuitem(self.custom_section,'something 1', 'app.work1')
self.add_simple_action(self.app,'work1',self.work1_cb)
def insert_custom_menu(self):
""" custom menu in child class"""
submenu1 = self.add_sub_menu('Miscellaneous next',self.menu_bar)
# # add section to submenu
section1 = self.add_section(submenu1)
# add item to section
self.add_menuitem(section1,'work hard', 'app.work')
self.add_simple_action(self.app,'work',self.work_cb)
def insert_custom_submenu_last(self):
""" in order to add something in the last submenu before Quit """
## current section is self.custom_section_last
self.add_menuitem(self.custom_section_last,'something in the last submenu', 'app.work2')
self.add_simple_action(self.app,'work2',self.work2_cb)
def work_cb(self, action, data=None):
print " It's very hard for me to make a class inherit class MyApp_root"
def work1_cb(self, action, data=None):
print " something 1"
def work2_cb(self, action, data=None):
print " ready to do something in the last submenu"
# to adapt a new help
def help_cb(self, action, data=None):
print "How to implement overwrite method in order to change action in child class here"
if __name__ == '__main__':
app = MyAppNew()
app.run(sys.argv)