简体   繁体   中英

Can anyone help me to find mistake in my python GUI code?

def square():
    def calculate():
        global area
        global perimeter
        side = entry_variable.get()
        area = side * side
        area_variable.set(area)
        perimeter = 4 * side
        perimeter_variable.set(perimeter)
root = Tk()
root.minsize(width=400, height=400)
root.maxsize(width=400, height=400)
root.config(bg="Black")
entry_variable = IntVar()
area_variable = IntVar()
perimeter_variable = IntVar()

label1 = Label(root, text="", font=('Arial', 15, 'bold'), fg="white", bg='Black')
label1.grid()
label0 = Label(root, text="Enter the Side's measurement", font=('Arial', 15, 'bold'), fg="white", bg='Black')
label0.grid(row=1)
label00 = Label(root, text="", font=('Arial', 15, 'bold'), fg="white", bg='Black')
label00.grid(row=3)
entry1 = Entry(root, font=('Arial', 15, 'bold'), justify=RIGHT, textvariable=entry_variable, fg='black')
entry1.grid(row=2)
label2 = Label(root, text="Perimeter of the Square", font=('Arial', 15, 'bold'), fg="white", bg='Black')
label2.grid(row=5)
label3 = Label(root, text="Area of the Square", font=('Arial', 15, 'bold'), fg="white", bg='Black')
label3.grid(row=7)
entry2 = Entry(root, font=('Arial', 15, 'bold'), fg='black', justify=RIGHT, textvariable=perimeter_variable,
               state='disabled')
entry2.grid(row=6)
entry3 = Entry(root, font=('Arial', 15, 'bold'), fg='black', justify=RIGHT, textvariable=area_variable,
               state='disabled')
entry3.grid(row=8)
btn = Button(root, font=('Arial', 15, 'bold'), text='Calculate', bd=10, command=calculate)
btn.grid(row=4)

I creating a basic GUI to calculate square area and method. I am using it in another GUI ( it's working perfectly) , I am accessing it using a button, that's why I created it a function. It don't give any error but it don't execute result also. I am a bigger so I am not much familiar OOPS!

It's not necessary to define two nested functions. Define one function in this way:

def calculate():

    side = entry_variable.get()
    area = side * side
    area_variable.set(area)
    perimeter = 4 * side
    perimeter_variable.set(perimeter)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM