如何让一个函数识别另一个函数? Python
How to make a function identify another? Python
所以我有这段代码,它创建了一个类似表单的形式供用户填写 Tkinter Canvas。在他们必须填写姓名的 space 中,如果他们输入的不是字母表,我希望我的代码在他们单击下一步按钮时显示错误消息。
这是一段相关的代码:
def check():
if not Name.isalpha():
messagebox.showerror('Only letters', 'Only letters are allowed!')
def Per_Form():
canvas.delete("all")
root.configure(bg="cornflower blue")
canvas.config(width=root.winfo_screenwidth(), height=root.winfo_screenheight(),
bg="cornflower blue") # to horizontally center the rectangle
form_title = canvas.create_rectangle(30, 20, 700, 75, fill="white", width=2)
canvas.move(form_title, 300, 30)
canvas.create_text(665, 70, text="Tell us more about you..")
FirstName = Label(canvas, text="First Name")
FirstName.configure(width=30, bg="white", fg="black", border=10)
FirstName = canvas.create_window(330, 130, anchor=NW, window=FirstName)
FName_Entry = Entry(canvas)
canvas.create_window(850, 145, window=FName_Entry, height=35, width=300)
Name = FName_Entry.get()
.
.
.
.
Next_button = Button(root, text="Next", anchor=CENTER, command=check)
Next_button.configure(width=10, bg="black", fg="blue", border=10)
Next_button = canvas.create_window(180, 200, anchor=NW, window=Next_button)
代码拒绝识别 Name 变量,因为它在 Per_Form
函数中。我该怎么办?
我已经尝试了几种变体,但它们都不起作用
编辑:
def check(Name):
if not Name.isalpha():
messagebox.showerror('Only letters', 'Only letters are allowed!')
else:
messagebox.showerror('Only letters', 'Perfect')
def Per_Form():
canvas.delete("all")
root.configure(bg="cornflower blue")
canvas.config(width=root.winfo_screenwidth(), height=root.winfo_screenheight(),
bg="cornflower blue") # to horizontally center the rectangle
form_title = canvas.create_rectangle(30, 20, 700, 75, fill="white", width=2)
canvas.move(form_title, 300, 30)
canvas.create_text(665, 70, text="Tell us more about you..")
FirstName = Label(canvas, text="First Name")
FirstName.configure(width=30, bg="white", fg="black", border=10)
FirstName = canvas.create_window(330, 130, anchor=NW, window=FirstName)
FName_Entry = Entry(canvas)
canvas.create_window(850, 145, window=FName_Entry, height=35, width=300)
Name = FName_Entry.get()
.
.
.
.
Next_button = Button(root, text="Next", anchor=CENTER, command=lambda: check(Name))
Next_button.configure(width=10, bg="black", fg="blue", border=10)
Next_button = canvas.create_window(180, 200, anchor=NW, window=Next_button)
首先你的 if-statement 不在你函数的封闭范围内,这会使你的函数为空并且会抛出错误。
def check(): if not Name.isalpha(): messagebox.showerror('Only letters', 'Only letters are allowed!')
然后你不在你的函数中解析参数名称所以在你的函数中名称将是未知的,这将引发错误。
def check(name):
那么你的问题是你需要解析参数但仍然不希望你的函数被立即调用。
Next button = Button(.., command= lambda: check(name))
它似乎存在的最后一个问题是,在您创建按钮时名称将不为人所知,因为您已经在函数中创建了该变量。但是只要我看不到您如何构建其余部分,我就无法为您解决这个问题。但我认为你可以处理这个。
所以我有这段代码,它创建了一个类似表单的形式供用户填写 Tkinter Canvas。在他们必须填写姓名的 space 中,如果他们输入的不是字母表,我希望我的代码在他们单击下一步按钮时显示错误消息。 这是一段相关的代码:
def check():
if not Name.isalpha():
messagebox.showerror('Only letters', 'Only letters are allowed!')
def Per_Form():
canvas.delete("all")
root.configure(bg="cornflower blue")
canvas.config(width=root.winfo_screenwidth(), height=root.winfo_screenheight(),
bg="cornflower blue") # to horizontally center the rectangle
form_title = canvas.create_rectangle(30, 20, 700, 75, fill="white", width=2)
canvas.move(form_title, 300, 30)
canvas.create_text(665, 70, text="Tell us more about you..")
FirstName = Label(canvas, text="First Name")
FirstName.configure(width=30, bg="white", fg="black", border=10)
FirstName = canvas.create_window(330, 130, anchor=NW, window=FirstName)
FName_Entry = Entry(canvas)
canvas.create_window(850, 145, window=FName_Entry, height=35, width=300)
Name = FName_Entry.get()
.
.
.
.
Next_button = Button(root, text="Next", anchor=CENTER, command=check)
Next_button.configure(width=10, bg="black", fg="blue", border=10)
Next_button = canvas.create_window(180, 200, anchor=NW, window=Next_button)
代码拒绝识别 Name 变量,因为它在 Per_Form
函数中。我该怎么办?
我已经尝试了几种变体,但它们都不起作用
编辑:
def check(Name):
if not Name.isalpha():
messagebox.showerror('Only letters', 'Only letters are allowed!')
else:
messagebox.showerror('Only letters', 'Perfect')
def Per_Form():
canvas.delete("all")
root.configure(bg="cornflower blue")
canvas.config(width=root.winfo_screenwidth(), height=root.winfo_screenheight(),
bg="cornflower blue") # to horizontally center the rectangle
form_title = canvas.create_rectangle(30, 20, 700, 75, fill="white", width=2)
canvas.move(form_title, 300, 30)
canvas.create_text(665, 70, text="Tell us more about you..")
FirstName = Label(canvas, text="First Name")
FirstName.configure(width=30, bg="white", fg="black", border=10)
FirstName = canvas.create_window(330, 130, anchor=NW, window=FirstName)
FName_Entry = Entry(canvas)
canvas.create_window(850, 145, window=FName_Entry, height=35, width=300)
Name = FName_Entry.get()
.
.
.
.
Next_button = Button(root, text="Next", anchor=CENTER, command=lambda: check(Name))
Next_button.configure(width=10, bg="black", fg="blue", border=10)
Next_button = canvas.create_window(180, 200, anchor=NW, window=Next_button)
首先你的 if-statement 不在你函数的封闭范围内,这会使你的函数为空并且会抛出错误。
def check(): if not Name.isalpha(): messagebox.showerror('Only letters', 'Only letters are allowed!')
然后你不在你的函数中解析参数名称所以在你的函数中名称将是未知的,这将引发错误。
def check(name):
那么你的问题是你需要解析参数但仍然不希望你的函数被立即调用。
Next button = Button(.., command= lambda: check(name))
它似乎存在的最后一个问题是,在您创建按钮时名称将不为人所知,因为您已经在函数中创建了该变量。但是只要我看不到您如何构建其余部分,我就无法为您解决这个问题。但我认为你可以处理这个。