Habe mich jetzt mal an einem größeren Programm versucht, scheitere aber jetzt daran, dass ich von der Menu-Klasse auf die Haupt-Klasse zugreifen möchte, was aber irgendwie nicht funktioniert.
Hier mal der Source:
main.py
Code: Alles auswählen
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
import menu as menu
class SGMain:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title('ShopGen')
window.maximize()
window.set_border_width(5)
content_box = gtk.VBox(False, 0)
ObjSGMenu = menu.SGMenu()
window.connect("delete_event", self.close_window)
content_box.show()
window.show()
def close_window(self, widget, event, data=None):
gtk.main_quit()
return 0
def main():
gtk.main()
if __name__ == '__main__':
ObjSGApp = SGMain()
main()
Code: Alles auswählen
import gtk
class SGMenu:
def __init__(self):
file_menu = gtk.Menu()
quit_item = gtk.MenuItem("Beenden")
file_menu.append(quit_item)
quit_item.connect("activate", self.menu_item_callback, "quit")
quit_item.show()
menu_bar = gtk.MenuBar()
ObjSGApp.window.add(menu_bar)
menu_bar.show()
file_item = gtk.MenuItem("File")
file_item.show()
menu_bar = gtk.MenuBar()
ObjSGApp.window.add(menu_bar)
menu_bar.show()
file_item.set_submenu(file_menu)
menu_bar.append(file_item)
def menu_item_callback(self, widget, data=None):
if data == "quit":
ObjSGApp.close_window(None, None, None)
Traceback (most recent call last):
File "main.py", line 33, in <module>
ObjSGApp = SGMain()
File "main.py", line 18, in __init__
ObjSGMenu = menu.SGMenu()
File "/home/scrawl/Dev/Shopgen 3.0/menu.py", line 14, in __init__
ObjSGApp.window.add(menu_bar)
NameError: global name 'ObjSGApp' is not defined
Mit "global ObjSGApp" hab ich es schon probiert, geht aber auch nicht
Freue mich auf Antworten
MfG