On Monday 29 September 2003 08:51 am, liquid wrote:
> Hi guys
> I'm really a newbie of python and pygtk, so sorry if my question is... too
> dumb (o:
Welcome to Python. No question is too dumb from a newbie:-) There mailing
lists specifically for these types of questions. As was mentioned earlier,
reviewing the Python tutorial is a very helpful first step.
http://www.python.org/doc/current/tut/tut.html
Also, the http://www.python.org/topics/learn/ page on the Python website has
lots of good references for beginners. You might also direct basic Python
questions to the Tutor mailing list.
http://mail.python.org/mailman/listinfo/tutor
Please browse through the archives to see if the questions have already been
asked. Also remember the PyGtk FAQ. It answers many basic questions.
http://www.async.com.br/faq/pygtk/
> I'm tring to understand how to set a value to a variable, with check
> buttons. I will post here what I'm doing...or better...tring to do.
> when I check the button I would like that the variable a will be set as 3
> and when is unchecked to 4, this of course doesn't work, when I press the
> other button to see the results I will get as output always the number 2,
> as set at the beginning.
> #!/usr/bin/env python
> import gtk
> a=2
> class try:
The example as written should generate a SyntaxError because the class name
'try' is a reserved keyword.
[EMAIL PROTECTED] src]$ python
Python 2.3 (#2, Aug 15 2003, 16:34:02)
[GCC 3.2.2 (Mandrake Linux 9.1 3.2.2-3mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class try:
File "<stdin>", line 1
class try:
^
SyntaxError: invalid syntax
>>>
The following corrections to your example fix the problem with 'a' not being
updated
#!/usr/bin/env python
import gtk
class foo:
def __init__(self):
#As was suggested, move 'a' to inside the class as an instance variable.
self.a=2
#Python searches the instance namespace first, then the class namespace, and
#finally the global namespace to resolve a name.
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("Check Button")
self.window.connect("delete_event", self.delete_event)
self.window.set_border_width(20)
table = gtk.Table(2, 2, gtk.TRUE)
self.window.add(table)
button = gtk.CheckButton("check button 1")
button.connect("toggled", self.callback, "check button 1")
table.attach(button, 1, 2, 0, 1)
button.show()
button=gtk.Button("press to try")
button.connect("clicked", self.call1, "cool button")
table.attach(button, 0, 1, 0, 1)
button.show()
table.show()
self.window.show()
def callback(self, widget, data=None):
# in you example the value 'a' was a local variable to the 'callback' method.
# Local variables are only in scope inside the method. The name
# local name and it's value disapears when the method returns.
# The following assignment converts the number to a string. This
# means that when you are printing out the initial value of 'a' it is
# printing a number and subsequent statements are printing a string
self.a = "%s" % ((3, 4)[widget.get_active()])
#Use the following statement to always assign an integer to self.a
#self.a = (3, 4)[widget.get_active()]
def call1(self, widget, data=None):
# The 'a' referenced in the print statement of your example was resolving to
# the global 'a', which had been assigned the value of 2. The reference to
# 'a' could not see the use of 'a' in the 'callback' method because that name
# went out of scope when the program returned from the method.
# The following print statement show what type of value is being printed
print "The value is:", self.a,'but it really is:', repr(self.a), type(self.a)
def delete_event(self, widget, event, data=None):
gtk.main_quit()
return gtk.FALSE
def main():
gtk.main()
return 0
if __name__ =="__main__":
foo()
main()
_______________________________________________
pygtk mailing list [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/