(我是該網站的新手,如果有任何錯誤,我很抱歉)這是代碼,我不知道為什么它一直崩潰我知道這不是我的 PC,因為我的許多其他 tkinter 程式都可以在它上面運行。我發布了所有代碼(不是很多),因為我不知道原因。謝謝你的幫助。
from tkinter import *
Calculator = Tk()
def input0():
int(input(0))
def input1():
int(input(1))
def input2():
int(input(2))
def input3():
int(input(3))
def input4():
int(input(4))
def input5():
int(input(5))
def input6():
int(input(6))
def input7():
int(input(7))
def input8():
int(input(8))
def input9():
int(input(9))
def inputplus():
str(input(" "))
def inputminus():
str(input("-"))
def inputdivide():
str(input("/"))
def inputtimes():
str(input("*"))
def inputdecimal():
str(input("."))
def equal():
eval
Calculator.geometry("237x336")
Num1 = Button(Calculator, text="1",command= input1, width=7, height=2)
Num1.place(x=1,y=80)
Num2 = Button(Calculator, text="2",command= input2, width=7, height=2)
Num2.place(x=60,y=80)
Num3 = Button(Calculator, text="3",command= input3, width=7, height=2)
Num3.place(x=120,y=80)
Num4 = Button(Calculator, text="4",command= input4, width=7, height=2)
Num4.place(x=1,y=40)
Num5 = Button(Calculator, text="5",command= input5, width=7, height=2)
Num5.place(x=60,y=40)
Num6 = Button(Calculator, text="6",command= input6, width=7, height=2)
Num6.place(x=120,y=40 )
Num7 = Button(Calculator, text="7",command= input7, width=7, height=2)
Num7.place(x=1,y=1)
Num8 = Button(Calculator, text="8",command= input8, width=7, height=2)
Num8.place(x=60,y=1)
Num9 = Button(Calculator, text="9",command= input9, width=7, height=2)
Num9.place(x=120,y=1)
Num0 = Button(Calculator, text="0",command= input0, width=7, height=2)
Num0.place(x=1,y=120)
x = Button(Calculator, text="x",command= inputtimes, width=7, height=2)
x.place(x=180,y=1)
divide = Button(Calculator, text="÷",command= inputdivide, width=7, height=2)
divide.place(x=180,y=40)
add = Button(Calculator, text=" ",command= inputplus, width=7, height=2)
add.place(x=180,y=80)
minus = Button(Calculator, text="-",command= inputminus, width=7, height=2)
minus.place(x=180,y=120)
dot = Button(Calculator, text=".",command= inputdecimal, width=7, height=2)
dot.place(x=120,y=120)
equals = Button(Calculator, text="=",command= eval, width=7, height=2)
equals.place(x=60,y=120)
Calculator.mainloop()
uj5u.com熱心網友回復:
事情是錯誤的:當你輸入時,你得到了用戶傳遞的文本,然后離開了。在equal()中,eval函式沒有引數。
固定代碼:
work = ''
from tkinter import *
Calculator = Tk()
def input0():
global work
# int(input(0))
work = work '0'
def input1():
global work
# int(input(1))
work = work '1'
def input2():
global work
# int(input(2))
work = work '2'
def input3():
global work
# int(input(3))
work = work '3'
def input4():
global work
# int(input(4))
work = work '4'
def input5():
global work
# int(input(5))
work = work '5'
def input6():
global work
# int(input(6))
work = work '6'
def input7():
global work
# int(input(7))
work = work '7'
def input8():
global work
# int(input(8))
work = work '8'
def input9():
global work
# int(input(9))
work = work '9'
def inputplus():
global work
# str(input(" "))
work = work ' '
def inputminus():
global work
# str(input("-"))
work = work '-'
def inputdivide():
global work
# str(input("/"))
work = work '/'
def inputtimes():
global work
# str(input("*"))
work = work '*'
def inputdecimal():
global work
# str(input("."))
work = work '.'
def equal():
global work
print(eval(work))
Calculator.geometry("237x336")
Num1 = Button(Calculator, text="1", command=input1, width=7, height=2)
Num1.place(x=1, y=80)
Num2 = Button(Calculator, text="2", command=input2, width=7, height=2)
Num2.place(x=60, y=80)
Num3 = Button(Calculator, text="3", command=input3, width=7, height=2)
Num3.place(x=120, y=80)
Num4 = Button(Calculator, text="4", command=input4, width=7, height=2)
Num4.place(x=1, y=40)
Num5 = Button(Calculator, text="5", command=input5, width=7, height=2)
Num5.place(x=60, y=40)
Num6 = Button(Calculator, text="6", command=input6, width=7, height=2)
Num6.place(x=120, y=40)
Num7 = Button(Calculator, text="7", command=input7, width=7, height=2)
Num7.place(x=1, y=1)
Num8 = Button(Calculator, text="8", command=input8, width=7, height=2)
Num8.place(x=60, y=1)
Num9 = Button(Calculator, text="9", command=input9, width=7, height=2)
Num9.place(x=120, y=1)
Num0 = Button(Calculator, text="0", command=input0, width=7, height=2)
Num0.place(x=1, y=120)
x = Button(Calculator, text="x", command=inputtimes, width=7, height=2)
x.place(x=180, y=1)
divide = Button(Calculator, text="÷", command=inputdivide, width=7, height=2)
divide.place(x=180, y=40)
add = Button(Calculator, text=" ", command=inputplus, width=7, height=2)
add.place(x=180, y=80)
minus = Button(Calculator, text="-", command=inputminus, width=7, height=2)
minus.place(x=180, y=120)
dot = Button(Calculator, text=".", command=inputdecimal, width=7, height=2)
dot.place(x=120, y=120)
equals = Button(Calculator, text="=", command=equal, width=7, height=2)
equals.place(x=60, y=120)
Calculator.mainloop()
現在,當您單擊“=”按鈕時,它將列印結果。不需要輸入,因為您通過單擊按鈕給出值
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/393607.html
上一篇:我怎樣才能增加分數?
