JAYDENCHEAH
Posts: 3
Joined: Tue Mar 08, 2016 2:49 pm

global name b2 is un defined

Sat Mar 12, 2016 7:49 am

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()

User avatar
DougieLawson
Posts: 36156
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: global name b2 is un defined

Sat Mar 12, 2016 10:36 am

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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
Paeryn
Posts: 2672
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: global name b2 is un defined

Sat Mar 12, 2016 12:20 pm

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.
She who travels light — forgot something.

texy
Forum Moderator
Forum Moderator
Posts: 5157
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: global name b2 is un defined

Mon Mar 14, 2016 1:26 pm

DougieLawson wrote:If you're going to post python code on the forum please use [code][/code] tags.
.
Fixed.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
RogerW
Posts: 285
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: global name b2 is un defined

Mon Mar 14, 2016 5:23 pm

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

Return to “Python”