python - Passing persistent values into a class -


using python 2.7 , tkinter in linux mint 'mate' 17 environment

i'm new oop , don't understand how pass persistent values class instance; in code "global not defined" error generated when use pin_id @ lines 20 , 22:

 1  #!/usr/bin/env python  2  import tkinter tk  3  4  root = tk.tk()  5  6  class cbclass:  7    def __init__(self, pin_id):  8      self.cb_txt=tk.stringvar()  9      self.cb_txt.set("pin " + pin_id + " off") 10      self.cb_var = tk.intvar() 11      cb = tk.checkbutton( 12        root, 13        textvariable=self.cb_txt, 14        variable=self.cb_var, 15        command=self.cbtest) 16      cb.pack() 17 18    def cbtest(self): 19      if self.cb_var.get(): 20        self.cb_txt.set("pin " + pin_id + " on") 21      else: 22        self.cb_txt.set("pin " + pin_id + " off") 23 24  c1 = cbclass("8") 25  c2 = cbclass("e") 26  root.mainloop() 

you want save pinid , class instance variable. in __init__

self.pinid = pinid 

and in cbtest, access self.pinid rather pinid


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -