Reputation: 7
I was trying to make a simple Simple Interest calculator using Pycharm and tkinter.
There is my code It is showing that x is not defined
I already tried to put variable as global.
Also I cant call that function in my finalProgram function because it makes it as endless loop
from tkinter import *
def mainWindow():
label = Label(frame, text = "What do you want to do")
label.pack()
but1 = Button(frame, text = "SI", command = SimpleInterest)
but1.pack()
def SimpleInterest():
global x
frame.destroy()
label5 = Label(frame2,text="Please enter principal amount" )
label5.pack()
p = Entry(frame2 )
p.pack()
label6 = Label(frame3,text="Please ROI")
label6.pack()
r = Entry(frame3, text="Please enter rate of interest")
r.pack()
label7 = Label(frame4, text="Please enter time")
label7.pack()
t = Entry(frame4, text="Please enter time")
t.pack()
buttonmain = Button(frame4, text = "Finlise", command = finalProgram)
buttonmain.pack()
global x
x =(p*r*t)/100
def finalProgram():
frame2.destroy()
frame3.destroy()
frame4.destroy()
global x
newlabel = Label(frame5, text = x)
root = Tk()
frame = Frame(root)
frame.grid(row=0,columnspan = 2)
mainWindow()
frame2 = Frame(root)
frame2.grid(row = 0, columnspan =2)
frame3 = Frame(root)
frame3.grid(row =1, columnspan =2)
frame4 = Frame(root)
frame4.grid(row=2, columnspan =2)
frame5 = Frame(root)
frame5.grid(row=0)
root.mainloop()
Upvotes: 0
Views: 53
Reputation: 2518
There is only one modification to your code. In the lower section one line is added:
x = 0.0
This is to ensure you have a variable x
in global scope. With that variable in global scope you are then able to use (read and write) it within your functions (after you declared it as global with global x
)
from tkinter import *
def mainWindow():
label = Label(frame, text = "What do you want to do")
label.pack()
but1 = Button(frame, text = "SI", command = SimpleInterest)
but1.pack()
def SimpleInterest():
global x
frame.destroy()
label5 = Label(frame2,text="Please enter principal amount" )
label5.pack()
p = Entry(frame2 )
p.pack()
label6 = Label(frame3,text="Please ROI")
label6.pack()
r = Entry(frame3, text="Please enter rate of interest")
r.pack()
label7 = Label(frame4, text="Please enter time")
label7.pack()
t = Entry(frame4, text="Please enter time")
t.pack()
buttonmain = Button(frame4, text = "Finlise", command = finalProgram)
buttonmain.pack()
global x
x =(p*r*t)/100
def finalProgram():
frame2.destroy()
frame3.destroy()
frame4.destroy()
global x
newlabel = Label(frame5, text = x)
x = 0.0
root = Tk()
frame = Frame(root)
frame.grid(row=0,columnspan = 2)
mainWindow()
frame2 = Frame(root)
frame2.grid(row = 0, columnspan =2)
frame3 = Frame(root)
frame3.grid(row =1, columnspan =2)
frame4 = Frame(root)
frame4.grid(row=2, columnspan =2)
frame5 = Frame(root)
frame5.grid(row=0)
root.mainloop()
Please note, that this is only a quick fix for the problem you asked. The general structure of you code is not really good and you should consider learning concepts like classes and/or how you pass arguments into functions.
Upvotes: 1