gtk3 - Python Gtk 3 window is not defined, class instancing confusion -
i starting python , utterly confused how object creation works. trying create user interface gtk. here example of problem having:
from gi.repository import gtk def button_clicked(self, button): self.button_label = button.get_label() if self.button_label == "login": window.quit() window2.start() class loginwindow(gtk.window): def __init__(self): gtk.window.__init__(self, title="amok cloud") self.connect("delete-event", gtk.main_quit) self.set_position(position = gtk.windowposition.center) # button self.loginbutton = gtk.button(label="login") self.loginbutton.connect("clicked", button_clicked(self, self.loginbutton)) self.add(self.loginbutton) self.show_all() gtk.main() def quit(self): self.close() gtk.main_quit() class mainwindow(gtk.window): def __init__(self): gtk.window.__init__(self, title="amok cloud") self.connect("delete-event", gtk.main_quit) self.set_position(position=gtk.windowposition.center) def start(self): self.show_all() gtk.main() window = loginwindow() window2 = mainwindow()
error comes nameerror: name 'window' not defined though did define window. don't understand. if can explain mean world me. in advance.
edit: guys works fine now, took gtk.main() out of both classes , added button_clicked method inside loginwindow() class , works charm. assumed needed gtk.main() every window open.
it because starting main loop (gtk.main()
) inside of loginwindow.__init__()
. means window = loginwindow()
line doesn't finish executing until after login window closed. should take gtk.main()
outside of __init__
method, , move last line in file. mentioned pm 2ring in comments, don't need call gtk.main()
twice. take out in mainwindow.start()
because 1 added last line in file takes care of that. mentioned pm, connect()
calls function when event happens. when give button_clicked(...)
, function called , telling connect()
call whatever returned, none
. if want special arguments, use lambda
, aren't changing (those default arguments anyway), can this:
self.connect("clicked", button_clicked)
i suggest instead of making button_clicked
separate function, make static method of class. placing inside class, @staticmethod
above def
line. way, makes sense take self
argument, don't need 2 parameters account same window.
Comments
Post a Comment