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)
我创建了一个基本的GUI来计算平方面积和方法。 我在另一个GUI中使用它(它工作正常),使用一个按钮访问它,这就是为什么我创建了一个函数。 它不给出任何错误,但也不执行结果。 我更大,所以我对OOPS不太熟悉!
答案 0 :(得分:0)
没有必要定义两个嵌套函数。以这种方式定义一个功能:
def calculate():
side = entry_variable.get()
area = side * side
area_variable.set(area)
perimeter = 4 * side
perimeter_variable.set(perimeter)