Question

I'm trying to write a script that will do check-box invisible when i click on main check-box.

This is code which i have written for that

#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk, cairo, gio, pango, pangocairo, atk, gobject

class form:
    def typeentry(self, widget, data):
        if data=="num" and widget.get_active()==True:
            self.buttonI.set_visible(True)
            self.buttonS.set_visible(True)
            self.buttonF.set_visible(True)
            self.buttonL.set_visible(True)
        if data=="num" and widget.get_active()==False:
            self.buttonI.set_visible(False)
            self.buttonS.set_visible(False)
            self.buttonF.set_visible(False)
            self.buttonL.set_visible(False)
    def __init__(self):
        self.win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.win.set_position(gtk.WIN_POS_CENTER)
        self.win.set_size_request(530, 340)
        self.win.set_resizable(False)

        evbox = gtk.VBox(False, 0)            
        frame = gtk.AspectFrame("Select", 1.0, 1.0,3, False)
        frame.set_border_width(10)
        vbox = gtk.VBox(False, 5)
        vbox.set_border_width(5)
        frame.add(vbox)
        vbox.set_uposition(90,165)
        button = gtk.CheckButton("Number")
        button.connect("toggled", self.typeentry, "num")
        vbox.pack_start(button, False, False, 2)
        button.set_active(True)

        button = gtk.CheckButton("String")
        vbox.pack_start(button, False, False, 0)
        button.set_active(True)

        self.buttonI = gtk.CheckButton("int")
        self.buttonI.connect("toggled", self.typeentry, "int")
        vbox.pack_start(self.buttonI, False, False, 0)
        self.buttonI.set_active(True)
        self.buttonI.set_uposition(300,155)

        self.buttonF = gtk.CheckButton("float")
        self.buttonF.connect("toggled", self.typeentry, "float")
        vbox.pack_start(self.buttonF, False, False, 0)
        self.buttonF.set_active(True)
        self.buttonF.set_uposition(300,170)

        self.buttonL = gtk.CheckButton("long")
        self.buttonL.connect("toggled", self.typeentry, "long")
        vbox.pack_start(self.buttonL, False, False, 0)
        self.buttonL.set_active(True)
        self.buttonL.set_uposition(300,185)

        self.buttonS = gtk.CheckButton("short")
        self.buttonS.connect("toggled", self.typeentry, "short")
        vbox.pack_start(self.buttonS, False, False, 0)
        self.buttonS.set_active(True)
        self.buttonS.set_uposition(300,200)

        evbox.add(frame)
        self.win.add(evbox)

        hbox = gtk.HBox(False, 0)
        evbox.pack_start(hbox, False, True, 5)
        hbox.set_uposition(0,295)

        button = gtk.Button("Previous")
        button.connect("clicked", lambda w: gtk.main_quit())
        button.set_size_request(90,35)
        hbox.pack_start(button, False, False,10)

        dbox = gtk.HBox(False, 0)
        button = gtk.Button("Next")
        button.connect("clicked", lambda w: gtk.main_quit())
        button.set_size_request(90,35)
        dbox.pack_end(button, True, True, 5)

        button = gtk.Button("Exit")
        button.set_size_request(90,35)
        dbox.pack_end(button, True, True,5)

        hbox.pack_end(dbox, False,True, 5)

        self.win.show_all()


    def main(self):
        gtk.main()

if __name__ == "__main__":
    first = form()
    first.main()

the code is running fine but at the time of running this code. it is showing below error

Traceback (most recent call last):
  File "H:\Eclipse Project\Sample\test.py", line 12, in typeentry
    self.buttonI.set_visible(True)
AttributeError: form instance has no attribute 'buttonI'

I am not understanding what is happening and what i do. please somebody help me for it.

Thanks

Was it helpful?

Solution 3

Actually you are calling typeentry function with another checkbox button which you have defined latter on that's why it is showing error. so first you have to define all the checkbox button then you can call the function.

just see the updated code, it is running fine.

#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk, cairo, gio, pango, pangocairo, atk, gobject

class form:
    def typeentry(self, widget, data):
        print "hi"
        if data=="num" and widget.get_active()==True:
            self.buttonS.set_visible(True)
            self.buttonI.set_visible(True)
            self.buttonF.set_visible(True)
            self.buttonL.set_visible(True)
        if data=="num" and widget.get_active()==False:
            self.buttonI.set_visible(False)
            self.buttonS.set_visible(False)
            self.buttonF.set_visible(False)
            self.buttonL.set_visible(False)
    def __init__(self):
        self.win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.win.set_position(gtk.WIN_POS_CENTER)
        self.win.set_size_request(530, 340)
        self.win.set_resizable(False)

        evbox = gtk.VBox(False, 0)            
        frame = gtk.AspectFrame("Select", 1.0, 1.0,3, False)
        frame.set_border_width(10)
        vbox = gtk.VBox(False, 5)
        vbox.set_border_width(5)
        frame.add(vbox)
        vbox.set_uposition(90,165)
        button1 = gtk.CheckButton("Number")
        vbox.pack_start(button1, False, False, 2)
        button1.set_active(True)

        button2 = gtk.CheckButton("String")
        vbox.pack_start(button2, False, False, 0)
        button2.set_active(True)

        self.buttonI = gtk.CheckButton("int")

        vbox.pack_start(self.buttonI, False, False, 0)
        self.buttonI.set_active(True)
        self.buttonI.set_uposition(300,155)

        self.buttonF = gtk.CheckButton("float")

        vbox.pack_start(self.buttonF, False, False, 0)
        self.buttonF.set_active(True)
        self.buttonF.set_uposition(300,170)

        self.buttonL = gtk.CheckButton("long")

        vbox.pack_start(self.buttonL, False, False, 0)
        self.buttonL.set_active(True)
        self.buttonL.set_uposition(300,185)

        self.buttonS = gtk.CheckButton("short")
        vbox.pack_start(self.buttonS, False, False, 0)
        self.buttonS.set_active(True)
        self.buttonS.set_uposition(300,200)

        button1.connect("clicked", self.typeentry, "num")
        self.buttonI.connect("clicked", self.typeentry, "int")
        self.buttonF.connect("clicked", self.typeentry, "float")
        self.buttonL.connect("clicked", self.typeentry, "long")
        self.buttonS.connect("clicked", self.typeentry, "short")
        evbox.add(frame)
        self.win.add(evbox)

        hbox = gtk.HBox(False, 0)
        evbox.pack_start(hbox, False, True, 5)
        hbox.set_uposition(0,295)

        button = gtk.Button("Previous")
        button.connect("clicked", lambda w: gtk.main_quit())
        button.set_size_request(90,35)
        hbox.pack_start(button, False, False,10)

        dbox = gtk.HBox(False, 0)
        button = gtk.Button("Next")
        button.connect("clicked", lambda w: gtk.main_quit())
        button.set_size_request(90,35)
        dbox.pack_end(button, True, True, 5)

        button = gtk.Button("Exit")
        button.set_size_request(90,35)
        dbox.pack_end(button, True, True,5)

        hbox.pack_end(dbox, False,True, 5)

        self.win.show_all()


    def main(self):
        gtk.main()

if __name__ == "__main__":
    first = form()
    first.main()

OTHER TIPS

self.typeentry() gets called when you set "Number" and String" checkbutton active properties. This is before the other buttons even exist.

 button = gtk.CheckButton("Number")
 button.connect("toggled", self.typeentry, "num")
 vbox.pack_start(button, False, False, 2)
 button.set_active(True)

The call to button.set_active triggers the toogled event and thus self.typeentry is called where you try to access a member of self.buttonI and others. At this point however self.buttonI and the other buttons haven't been defined yet.

Instead add the event handler later (you don't need it while initializing):

 button = gtk.CheckButton("Number")
 vbox.pack_start(button, False, False, 2)
 button.set_active(True)
 button.connect("toggled", self.typeentry, "num")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top