我在python中的代码有什么问题?

2024-03-29 05:35:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在关注tkinter的在线教程,这是其中一行代码:

button1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))

这是我运行此操作时它给我的错误:

invalid syntax (<unknown>, line 18)

我需要修理什么

以下是我编写的所有代码:

from tkinter import *
root = Tk()
root.title("Calculator")

e = Entry(root, width =35, borderwidth=5)
e.grid(row=0, column=0, columnspan = 3, padx =10, pady=10)

# e.insert(0, "Enter Your Name: ")

def button_click(number):
    current = e.get()
    e.insert(0,str(current) + str(number)
    


#Define Buttons

button1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button2 = Button(root, text='2', padx=40, pady=20, command=lambda: button_click(2))
button3 = Button(root, text='3', padx=40, pady=20, command=lambda: button_click(3))
button4 = Button(root, text='4', padx=40, pady=20, command=lambda: button_click(4))
button5 = Button(root, text='5', padx=40, pady=20, command=lambda: button_click(5))
button6 = Button(root, text='6', padx=40, pady=20, command=lambda: button_click(6))
button7 = Button(root, text='7', padx=40, pady=20, command=lambda: button_click(7))
button8 = Button(root, text='8', padx=40, pady=20, command=lambda: button_click(8))
button9 = Button(root, text='9', padx=40, pady=20, command=lambda: button_click(9))
button0 = Button(root, text='0', padx=40, pady=20, command=lambda: button_click(0))

buttonadd = Button(root, text='+', padx=39, pady=20, command=lambda: button_click())
buttonequal = Button(root, text='=', padx=91, pady=20, command=lambda: button_click())
buttonclear = Button(root, text='C', padx=79, pady=20, command=lambda: button_click())
#Put the Buttons on the screen

button1.grid(row=3, column=0)
button2.grid(row=3, column=1)
button3.grid(row=3, column=2)

button4.grid(row=2, column=0)
button5.grid(row=2, column=1)
button6.grid(row=2, column=2)

button7.grid(row=1, column=0)
button8.grid(row=1, column=1)
button9.grid(row=1, column=2)

button0.grid(row=4, column=0)

buttonclear.grid(row=4, column=1, columnspan=2)
buttonadd.grid(row=5, column=0)
buttonequal.grid(row=5, column=1, columnspan=2)





root.mainloop()

Tags: lambdatexttkintercolumnbuttonrootcommandgrid
2条回答

这是你的问题;你没有关闭括号

e.insert(0,str(current) + str(number)

此处缺少括号:

e.insert(0,str(current) + str(number)

应该是:

e.insert(0,str(current) + str(number))

相关问题 更多 >