Reputation: 31
been having a problem with this program since this morning, it seems to just be placement of code rather than the code itself, I am trying to run a calculator in a new tkinter window separate from the root window and have been juggling the code around to try and get it to function to no avail, the errors that keep popping up seem to be based on "typeSpace" or "calcWindow" not being defined but they clearly are, does anybody know how to correctly place this code so it recognizes these variables correctly at run time?
from tkinter import *
import tkinter as tk
#def onButton_Plus():
# global one_Num#Declares global variable
# number_One = typeSpace.get()#Gets number typed on calculator by user
# one_Num = int(number_One)
# typeSpace.delete(0, END)
def calcClick():
calcWindow = Toplevel()##opens a new window for Calculator mode
##Creates a empty field in the calculator page for numbers to be displayed
typeSpace = Entry(calcWindow, width=35, borderwidth=5)
typeSpace.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
##Names the new window
calcWindow.title("Calculator Mode")
##Defines calculator buttons and places them in grid setting on screen
button_1 = Button(calcWindow, text="1", padx=40, pady=20, command=lambda: onButton_Click(1)).grid(row=3, column=0)
button_2 = Button(calcWindow, text="2", padx=40, pady=20, command=lambda: onButton_Click(2)).grid(row=3, column=1)
button_3 = Button(calcWindow, text="3", padx=40, pady=20, command=lambda: onButton_Click(3)).grid(row=3, column=2)
button_4 = Button(calcWindow, text="4", padx=40, pady=20, command=lambda: onButton_Click(4)).grid(row=2, column=0)
button_5 = Button(calcWindow, text="5", padx=40, pady=20, command=lambda: onButton_Click(5)).grid(row=2, column=1)
button_6 = Button(calcWindow, text="6", padx=40, pady=20, command=lambda: onButton_Click(6)).grid(row=2, column=2)
button_7 = Button(calcWindow, text="7", padx=40, pady=20, command=lambda: onButton_Click(7)).grid(row=1, column=0)
button_8 = Button(calcWindow, text="8", padx=40, pady=20, command=lambda: onButton_Click(8)).grid(row=1, column=1)
button_9 = Button(calcWindow, text="9", padx=40, pady=20, command=lambda: onButton_Click(9)).grid(row=1, column=2)
button_0 = Button(calcWindow, text="0", padx=40, pady=20, command=lambda: onButton_Click(0)).grid(row=4, column=0)
button_plus = Button(calcWindow, text="+", padx=39, pady=20, command=lambda: onButton_Click()).grid(row=5, column=0)
button_equal = Button(calcWindow, text="=", padx=91, pady=20, command=lambda: onButton_Click()).grid(row=5, column=1,columnspan=2)
button_clear = Button(calcWindow, text="Clear", padx=79, pady=20, command= onButton_Clear()).grid(row=4, column=1, columnspan=2)
def tiClick():
tInputWindow = Toplevel()##opens a new window for the text input mode
tInputWindow.title("Text Input Mode")##Names the new window
def onButton_Click(number):
onNo = typeSpace.get()##gets the number pressed by the user
typeSpace.delete(0, END)##deletes previous numbers typed
typeSpace.insert(0, str(onNo) + str(number))##Inserts the number clicked into the field
def onButton_Clear():##Defines what the clear button does when clicked
typeSpace.delete(0, END)##deletes all previous numbers typed
root = Tk()#Creates root widget
root.title("Welcome Screen")#Names the welcome screen
welcomeLabel = Label (root, text="Hello user, welcome to the text input and calculator program")#Creates label widget
welcomeLabel.grid(row=0, column=0)#Casts label to screen in specified position
textInputButton = Button(root, text="Text Input Mode", command=tiClick, fg='red')#adds button
textInputButton.grid(row=1, column=10)#Positions button
calc = Button(root, text="Calculator Mode", command=calcClick, fg='red')#adds button
calc.grid(row=2, column=10)#Positions button
root.mainloop()
Upvotes: 0
Views: 51
Reputation: 207
When you define a variable inside a function its use is limited only to that function. Here you have defined typeSpace
and calcWindow
in the calcClick()
function ,so typeSpace
is a local variable and thus when you make any changes to typeSpace
in a function other than calcClick()
function, the program returns the error of typeSpace
not being defined.
In order to use a local variable outside the funtion it is defined in you just need to give it the status of a global variable :
global calcWindow
calcWindow = Toplevel()
global typeSpace
typeSpace = Entry(calcWindow, width=35, borderwidth=5)
typeSpace.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
button_plus
, button_equal
and button_clear
will not work. The function onButton_Click(number)
will not accept empty entries. Also if you insert a value other than a number (like +,= or clear).
.
button_plus = Button(calcWindow, text="+", padx=39, pady=20, command=lambda: onButton_Click(+)).grid(row=5, column=0)
button_equal = Button(calcWindow, text="=", padx=91, pady=20, command=lambda: onButton_Click("=")).grid(row=5, column=1,columnspan=2)
button_clear = Button(calcWindow, text="Clear", padx=79, pady=20, command= onButton_Clear("Clear")).grid(row=4, column=1, columnspan=2)
.
.
The program will return error
Refer to this if you get in any trouble again
from tkinter import *
import tkinter as tk
#TEXT INPUT MODE
def tiClick():
tInputWindow = Toplevel()##opens a new window for the text input mode
tInputWindow.title("Text Input Mode")##Names the new window
#CALCULATOR MODE
def onButton_Plus():
global one_Num #Declares global variable
number_One = typeSpace.get()#Gets number typed on calculator by user
one_Num = int(number_One)
typeSpace.delete(0, END)
def calcClick():
calcWindow = Toplevel()##opens a new window for Calculator mode
##Creates a empty field in the calculator page for numbers to be displayed
global typeSpace
typeSpace = Entry(calcWindow, width=35, borderwidth=5)
typeSpace.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
##Names the new window
calcWindow.title("Calculator Mode")
##Defines calculator buttons and places them in grid setting on screen
button_1 = Button(calcWindow, text="1", padx=40, pady=20, command=lambda: onButton_Click(1)).grid(row=3, column=0)
button_2 = Button(calcWindow, text="2", padx=40, pady=20, command=lambda: onButton_Click(2)).grid(row=3, column=1)
button_3 = Button(calcWindow, text="3", padx=40, pady=20, command=lambda: onButton_Click(3)).grid(row=3, column=2)
button_4 = Button(calcWindow, text="4", padx=40, pady=20, command=lambda: onButton_Click(4)).grid(row=2, column=0)
button_5 = Button(calcWindow, text="5", padx=40, pady=20, command=lambda: onButton_Click(5)).grid(row=2, column=1)
button_6 = Button(calcWindow, text="6", padx=40, pady=20, command=lambda: onButton_Click(6)).grid(row=2, column=2)
button_7 = Button(calcWindow, text="7", padx=40, pady=20, command=lambda: onButton_Click(7)).grid(row=1, column=0)
button_8 = Button(calcWindow, text="8", padx=40, pady=20, command=lambda: onButton_Click(8)).grid(row=1, column=1)
button_9 = Button(calcWindow, text="9", padx=40, pady=20, command=lambda: onButton_Click(9)).grid(row=1, column=2)
button_0 = Button(calcWindow, text="0", padx=40, pady=20, command=lambda: onButton_Click(0)).grid(row=4, column=0)
button_plus = Button(calcWindow, text="+", padx=39, pady=20, command=onButton_Plus).grid(row=5, column=0)
button_equal = Button(calcWindow, text="=", padx=91, pady=20, command=onButton_Equals).grid(row=5, column=1,columnspan=2)
button_clear = Button(calcWindow, text="Clear", padx=79, pady=20, command= onButton_Clear).grid(row=4, column=1, columnspan=2)
def onButton_Click(number):
onNo = typeSpace.get()##gets the number pressed by the user
typeSpace.delete(0, END)##deletes previous numbers typed
typeSpace.insert(0, str(onNo) + str(number))##Inserts the number clicked into the field
def onButton_Clear():##Defines what the clear button does when clicked
typeSpace.delete(0, END)##deletes all previous numbers typed
def onButton_Equals():
sec_Num = typeSpace.get()
typeSpace.delete(0, END)
sec_Num = int(sec_Num)
ANSWER = one_Num+sec_Num
typeSpace.insert(0, ANSWER)
root = Tk()#Creates root widget
root.title("Welcome Screen")#Names the welcome screen
welcomeLabel = Label (root, text="Hello user, welcome to the text input and calculator program")#Creates label widget
welcomeLabel.grid(row=0, column=0)#Casts label to screen in specified position
textInputButton = Button(root, text="Text Input Mode", command=tiClick, fg='red')#adds button
textInputButton.grid(row=1, column=10)#Positions button
calc = Button(root, text="Calculator Mode", command=calcClick, fg='red')#adds button
calc.grid(row=2, column=10)#Positions button
root.mainloop()
Hope it was helpful...
Upvotes: 3
Reputation: 2270
Here. I reworked your code and made it less messy.
Code:
from tkinter import *
#def onButton_Plus():
# global one_Num#Declares global variable
# number_One = typeSpace.get()#Gets number typed on calculator by user
# one_Num = int(number_One)
# typeSpace.delete(0, END)
def calcClick():
calcWindow = Toplevel()
typeSpace = Entry(calcWindow, width=35, borderwidth=5)
typeSpace.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
calcWindow.title("Calculator Mode")
def onButton_Click(number):
onNo = typeSpace.get()##gets the number pressed by the user
typeSpace.delete(0, END)##deletes previous numbers typed
typeSpace.insert(0, str(onNo) + str(number))##Inserts the number clicked into the field
def equal_clicked():
print(eval(typeSpace.get()))
def onButton_Clear():##Defines what the clear button does when clicked
typeSpace.delete(0, END)
##Defines calculator buttons and places them in grid setting on screen
button_1 = Button(calcWindow, text="1", padx=40, pady=20, command=lambda: onButton_Click(1)).grid(row=3, column=0)
button_2 = Button(calcWindow, text="2", padx=40, pady=20, command=lambda: onButton_Click(2)).grid(row=3, column=1)
button_3 = Button(calcWindow, text="3", padx=40, pady=20, command=lambda: onButton_Click(3)).grid(row=3, column=2)
button_4 = Button(calcWindow, text="4", padx=40, pady=20, command=lambda: onButton_Click(4)).grid(row=2, column=0)
button_5 = Button(calcWindow, text="5", padx=40, pady=20, command=lambda: onButton_Click(5)).grid(row=2, column=1)
button_6 = Button(calcWindow, text="6", padx=40, pady=20, command=lambda: onButton_Click(6)).grid(row=2, column=2)
button_7 = Button(calcWindow, text="7", padx=40, pady=20, command=lambda: onButton_Click(7)).grid(row=1, column=0)
button_8 = Button(calcWindow, text="8", padx=40, pady=20, command=lambda: onButton_Click(8)).grid(row=1, column=1)
button_9 = Button(calcWindow, text="9", padx=40, pady=20, command=lambda: onButton_Click(9)).grid(row=1, column=2)
button_0 = Button(calcWindow, text="0", padx=40, pady=20, command=lambda: onButton_Click(0)).grid(row=4, column=0)
button_plus = Button(calcWindow, text="+", padx=39, pady=20, command=lambda: onButton_Click('+')).grid(row=5, column=0)
button_equal = Button(calcWindow, text="=", padx=91, pady=20, command=lambda: equal_clicked()).grid(row=5, column=1,columnspan=2)
button_clear = Button(calcWindow, text="Clear", padx=79, pady=20, command= lambda: onButton_Clear()).grid(row=4, column=1, columnspan=2)
def tiClick():
tInputWindow = Toplevel()##opens a new window for the text input mode
tInputWindow.title("Text Input Mode")##Names the new window
root = Tk()#Creates root widget
root.title("Welcome Screen")#Names the welcome screen
welcomeLabel = Label (root, text="Hello user, welcome to the text input and calculator program")#Creates label widget
welcomeLabel.grid(row=0, column=0)#Casts label to screen in specified position
textInputButton = Button(root, text="Text Input Mode", command=tiClick, fg='red')#adds button
textInputButton.grid(row=1, column=10)#Positions button
calc = Button(root, text="Calculator Mode", command=calcClick, fg='red')#adds button
calc.grid(row=2, column=10)#Positions button
root.mainloop()
When you click the equals button, the result is printed to the shell.
Hope this helps!
Upvotes: 1