我使用Tkinter在python中制作了一个简单的GUI。 我写它的目的是,当一个按钮被按下时,一个函数(度量)被调用。 我现在尝试绑定Enter键来执行此功能,方法是使用:
root.bind("<Return>", measure)
按Enter键时会出现错误:
^{pr2}$快速搜索告诉我,如果我给函数提供参数(self),那么enter bind将起作用,但是如果我这样做,那么按钮小部件会给出错误:
TypeError: measure() takes exactly 1 positional argument (0 given)
有什么快速解决办法吗? Python初学者,如果这是一个非常简单的问题,请道歉。在
import datetime
import csv
from tkinter import *
from tkinter import messagebox
root = Tk()
winx = 480
winy = 320
virtual_reading = '1.40mm'
def measure():
todays_date = datetime.date.today()
try:
get_tool_no = int(tool_no_entry.get())
if get_tool_no <= 0:
messagebox.showerror("Try Again","Please Enter A Number")
else:
with open("thickness records.csv", "a") as thicknessdb:
thicknessdbWriter = csv.writer(thicknessdb, dialect='excel', lineterminator='\r')
thicknessdbWriter.writerow([get_tool_no] + [todays_date] + [virtual_reading])
thicknessdb.close()
except:
messagebox.showerror("Try Again","Please Enter A Number")
tool_no_entry.delete(0, END)
root.resizable(width=FALSE, height=FALSE)
root.geometry('%dx%d' % (winx,winy))
root.title("Micrometer Reader V1.0")
record_button = Button(root,width = 30,
height = 8,
text='Measure',
fg='black',
bg="light grey", command = measure)
record_button.place(x = 350, y = 100, anchor = CENTER)
reading_display = Label(root, font=("Helvetica", 22), text = virtual_reading)
reading_display.place(x = 80, y =80)
tool_no_entry = Entry(root)
tool_no_entry.place(x = 120, y = 250, anchor=CENTER)
tool_no_entry.focus_set()
root.bind("<Return>", measure)
root.mainloop()
command
调用没有参数的measure
,但是bind
用event
参数调用它,所以measure
必须接收这个值。在你可以用
它将与}一起工作
command
和{您使用了函数
measure
两次,一次需要一个参数,第二次需要0参数。你应该用lambda包起来:相关问题 更多 >
编程相关推荐