-1

首先我有一个工作正常的 sqrt 按钮,然后我添加了 pi 按钮,但没有任何效果,我尝试更改所有内容,但我仍然不知道出了什么问题!请有人帮忙。

import sys
from tkinter import *
from math import *

def sqrt_():
    text = ment.get()
    a = sqrt(text)
    label['text'] = a

def pi_():
    text = ment.get()
    a = pi(text)
    label_1['text'] = a

root = Tk()
root.title('Conversions')
root.geometry('400x400')

#Get square root
sqrt_button = Button(root, text='Get Square root',command= sqrt_).place(x='160', y='5')
label = Label(root, text='')
label.place(x=5, y=30)
ment = IntVar()
entry = Entry(textvariable=ment).place(x='5', y= '10 ')
#Get Pi
pi_button = Button(root, text='Get Pi',command= pi_).place(x='160', y='50')
label_1 = Label(root, text='')
label_1.place(x=55, y=200)
ment = IntVar()
entry_1 = Entry(textvariable=ment).place(x='5', y= '55 ')


root.mainloop()
4

2 回答 2

1

首先,您没有定义功能pi,这意味着当您单击第二个按钮时它将失败。

其次,您重新定义了ment. 在这种情况下,两个条目都将绑定到同一个 int。这意味着当您单击第一个按钮时,它将从第二个条目中读取值。所以将所有第二个更改mentment_1. 名称、条目中的名称和 中的名称pi_

import sys
from tkinter import *
from math import *

def sqrt_():
    text = ment.get()
    a = sqrt(text)
    label['text'] = a

def pi_():
    label_1['text'] = pi

root = Tk()
root.title('Conversions')
root.geometry('400x400')

#Get square root
sqrt_button = Button(root, text='Get Square root',command= sqrt_).place(x='160', y='5')
label = Label(root, text='')
label.place(x=5, y=30)
ment = IntVar()
entry = Entry(textvariable=ment).place(x='5', y= '10 ')
#Get Pi
pi_button = Button(root, text='Get Pi',command= pi_).place(x='160', y='50')
label_1 = Label(root, text='')
label_1.place(x=55, y=200)
ment_1 = IntVar()
entry_1 = Entry(textvariable=ment_1).place(x='5', y= '55 ')


root.mainloop()
于 2013-07-27T10:34:24.373 回答
0

pi 不是一个函数,它是一个常数,所以:

def pi_():
    label_1['text'] = pi
于 2013-07-27T10:28:26.923 回答