Page 1 of 1

global name b2 is un defined

Posted: Sat Mar 12, 2016 7:49 am
by JAYDENCHEAH
Hi everyone, looking for help from you all. Currently i am facing a problem when i want to forget a button in tkinter by using python. But when i run the program, it shows me global name b2 is undefined. Can anyone explain to me why i can forget b1 and b3 but cannot forget b2? Below is my simple code. PLease forgive me, i am beginner. Please help me.

Code: Select all

from Tkinter import*
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

GPIO.setup(18,GPIO.OUT)
GPIO.output(18,0)

app=Tk()

def page1():
    print("1")
    checkpg1()

def page2():
    print("2")
    checkpg2()

def page3():
    print("3")
    checkpg1()


def checkpg1():

    if(GPIO.input(12)==1):
                GPIO.output(18,1)
                b2=Button(app,text="call", command=page2)
                b2.pack()
                b1.pack_forget()
                b3.pack_forget()
    else:
                GPIO.output(18,0)

def checkpg2():

    if(GPIO.input(12)==1):
                GPIO.output(18,1)
                b2.pack_forget()
                b1=Button(app,text="Jayden", command=page1)
                b3=Button(app,text="daryyl", command=page3)
                b1.pack()
                b3.pack()
                
                
    else:
                GPIO.output(18,0)

b1=Button(app,text="Jayden", command=page1)
b1.pack()

b3=Button(app,text="daryyl", command=page3)
b3.pack()


mainloop()

Re: global name b2 is un defined

Posted: Sat Mar 12, 2016 10:36 am
by DougieLawson
If you're going to post python code on the forum please use [code][/code] tags.

That makes the difference between
if code_tags <> "used":
print "all formatting is lost"
else:
print "all formatting is retained"

and

Code: Select all

if code_tags <> "used":
  print "all formatting is lost"
else:
  print "all formatting is retained"
Please edit your post to fix that.

Re: global name b2 is un defined

Posted: Sat Mar 12, 2016 12:20 pm
by Paeryn
From the looks of it (guessing the intended indentation), b2 is used in checkpg2 but never created anywhere other than in checkpg1 (where it will be local to that function). I assume b1 and b3 are created outside of checkpg2. If you're going to use globals you should be declaring them as global in the functions that use them.

As Dougie said, without being able to see the indentation it's not clear what belongs where.

Re: global name b2 is un defined

Posted: Mon Mar 14, 2016 1:26 pm
by texy
DougieLawson wrote:If you're going to post python code on the forum please use [code][/code] tags.
.
Fixed.
Texy

Re: global name b2 is un defined

Posted: Mon Mar 14, 2016 5:23 pm
by RogerW
You need to understand the concept of scope.

b2 is defined in checkpg1 but is not visible outside that function.

b1 is defined as a global near the end of the program but also as a local variable in checkpg2.

Code: Select all

def foo():
    #global x
    x = 9

x = 3
print(x)

foo()

print(x)
Run this little program and it will print 3 twice. Uncomment the global statement in foo and it will print 3 and 9