Chaudry Shahid Iqbal
Chaudry Shahid Iqbal

Reputation: 29

button1 is not defined?

This is my code :

import sys
from tkinter import *


#first new screen
def hypoténusegetdef ():
    widgets1 = button1
    nextscreen1(widgets1)

def next_screen1(names):
    for widget in names:
        widget.place_forget() 
        hyplabel1 = Label (text = "This is my text")



def next_screen(names):
    for widget in names:
        widget.place_forget()
        button1 = Button (text = "Button1",fg = "blue",command = hypoténusegetdef)
        button1.grid (row = 1,column = 2)


def forget_page1():
    widgets = [mLabel1, button]
    next_screen(widgets)

################################################################################

#first page things
mGui = Tk ()

mGui.geometry("600x600+545+170")
mGui.title("MyMathDictionary")
mLabel1 = Label (text = "Welcome to MyMathDictionary. Press Next to continue.",
                 fg = "blue",bg = "white")
mLabel1.place (x= 150,y = 200)

button = Button (text = "Next", command = forget_page1 )
button.place(x = 275,y = 230)

mGui.mainloop()

I want the user to click on "next" and then a button appears caled "button1" after clicking on that button a text should appear "this is my text" but it gives me an error:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "C:\Python33\Projects\MyMathDictionary.py", line 7, in hypoténusegetdef
widgets1 = button1
NameError: global name 'button1' is not defined

Any help would be apreciated :

Upvotes: 0

Views: 157

Answers (1)

Shish
Shish

Reputation: 2970

button1 is defined in next_screen, but used in hypoténusegetdef -- you can't use a variable from one function inside another. Looking at the rest of the code, the simplest thing would probably be to use a global variable that can be accessed anywhere (generally bad practice, but for short scripts they can make things easier)

Upvotes: 1

Related Questions