Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查用户在python中的输入_Python_User Input_Calculator - Fatal编程技术网

如何检查用户在python中的输入

如何检查用户在python中的输入,python,user-input,calculator,Python,User Input,Calculator,我正在尝试用python制作一个计算器,所以我需要这种机制,当用户输入一个数字时,我显示它,我希望它被保存,然后当他单击例如加号时,屏幕应该再次变为空白,他输入其他数字,当他单击相等时,它被添加 PS:请只检查有问题的badd()和beql()函数,为什么没有调用函数bc() 我的代码: from Tkinter import* #function for about menu def about(): win2=Tk() win2.wm_title("About

我正在尝试用python制作一个计算器,所以我需要这种机制,当用户输入一个数字时,我显示它,我希望它被保存,然后当他单击例如加号时,屏幕应该再次变为空白,他输入其他数字,当他单击相等时,它被添加

PS:请只检查有问题的badd()和beql()函数,为什么没有调用函数bc()

我的代码:

    from Tkinter import*



#function for about menu 
def about():
    win2=Tk()
    win2.wm_title("About")
    l=Label(win2,text=" CAL-C ver 1.0 \n Developer. \n Mohd Sanad",padx=5,pady=40)
    l.pack()
st=""
def but1():
    global st
    st=st+"1"
    v.set(st)
def but2():
    global st
    st=st+"2"
    v.set(st)
def but3():
    global st
    st=st+"3"
    v.set(st)
def but4():
    global st
    st=st+"4"
    v.set(st)
def but5():
    global st
    st=st+"5"
    v.set(st)
def but6():
    global st
    st=st+"6"
    v.set(st)
def but7():
    global st
    st=st+"7"
    v.set(st)
def but8():
    global st
    st=st+"8"
    v.set(st)
def but9():
    global st
    st=st+"9"
    v.set(st)
def but10():
    global st
    st=st+"0"
    v.set(st)
def bc():
    global st
    v.set("")
    st=""
def beql(x):
        bc()
        v.set(str(x))
def badd():
        x=int(st)
        bc()
        y=int(v.get())
        sum=x+y
        beql(sum)




win=Tk()#creating window
win.title("Calculator")#changing window title
win.config(background= "white")
#Initiliazing about menu button
menubar=Menu(win,bg="brown")
menubar.add_command(label="About",command=about)
win.config(menu=menubar)


#TextField
v=StringVar()
e=Entry(win,textvariable=v)
e.grid(row=0,column=0,columnspan=3,sticky=W+E)
#Initializing buttons
b1=Button(win,text="1",bg="red")
b2=Button(win,text="2",bg="red")
b3=Button(win,text="3",bg="red")
b4=Button(win,text="4",bg="red")
b5=Button(win,text="5",bg="red")
b6=Button(win,text="6",bg="red")
b7=Button(win,text="7",bg="red")
b8=Button(win,text="8",bg="red")
b9=Button(win,text="9",bg="red")
b10=Button(win,text="0",bg="red")
bdec=Button(win,text=".",bg="orange")
badd=Button(win,text="+",bg="yellow")
bsub=Button(win,text="-",bg="yellow")
bmul=Button(win,text="x",bg="yellow")
bdiv=Button(win,text="/",bg="yellow")
bclear=Button(win,text="CS",bg="blue")
beql=Button(win,text="=",bg="orange")
#Positioning Buttons

b1.grid(row=1,column=0,sticky=W+E)
b2.grid(row=1,column=1,sticky=W+E)
b3.grid(row=1,column=2,sticky=W+E)
b4.grid(row=2,column=0,sticky=W+E)
b5.grid(row=2,column=1,sticky=W+E)
b6.grid(row=2,column=2,sticky=W+E)
b7.grid(row=3,column=0,sticky=W+E)
b8.grid(row=3,column=1,sticky=W+E)
b9.grid(row=3,column=2,sticky=W+E)
b10.grid(row=4,column=0,sticky=W+E)
bdec.grid(row=4,column=1,sticky=W+E)
badd.grid(row=1,column=3,sticky=W+E)
bsub.grid(row=2,column=3,sticky=W+E)
bmul.grid(row=3,column=3,sticky=W+E)
bdiv.grid(row=4,column=3,sticky=W+E)
bclear.grid(row=0,column=3,sticky=W+E)
beql.grid(row=4,column=2,sticky=W+E)
#configuring button behaviour
b1.config(command=but1)
b2.config(command=but2)
b3.config(command=but3)
b4.config(command=but4)
b5.config(command=but5)
b6.config(command=but6)
b7.config(command=but7)
b8.config(command=but8)
b9.config(command=but9)
b10.config(command=but10)
bclear.config(command=bc)
beql.config(command=beql)
badd.config(command=badd)

当您将按钮定义为具有相同名称时,您正在覆盖
badd
beql
函数

...

def beql(x):
    bc()
    v.set(str(x))
def badd():
    x=int(st)
    bc()
    y=int(v.get())
    sum=x+y
    beql(sum)

# beql and badd are now functions

...

badd=Button(win,text="+",bg="yellow")
beql=Button(win,text="=",bg="orange")
# beql and badd are now Button instances

beql.config(command=beql)
badd.config(command=badd)
# configure the button to call the Button instance when pressed.

你会犯什么样的错误?另外,第二个块中的缩进是代码中的缩进还是复制粘贴错误?没有错误第二个代码完全不工作,我没有得到所需的输出,它的复制粘贴错误请重新检查我的问题谢谢:)什么是
st
x
v
,等等。?请发布一个完整的例子。是的,这就是我们想要的计算器,如果你输入一个10位数的数字,应该是这样的吗?