如何使一个函数识别另一个?Python [英] How to make a function identify another? Python

查看:50
本文介绍了如何使一个函数识别另一个?Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这段代码,可以创建某种形式,供用户在Tkinter画布上填写.在他们必须填写姓名的空格中,如果输入的不是字母,我希望我的代码在单击下一步时显示错误消息.这是一段相关的代码:

So I have this code which create sort of like a form for users to fill on a Tkinter Canvas. In the space where they have to fill in their name, if they enter anything that is not an alphabet, I want my code to display an error message when they click the next button. Here is a relevant piece of the code:

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 函数中一样.我该怎么办?

The code refuses to recognize the Name variable as it is in the Per_Form function. What do I do?

我已经尝试了几种变体,但是它们不起作用

I have tried several variations of this but they dont work

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语句不在函数的封闭范围内,这会使函数为空,并且将引发错误. def check():如果不是Name.isalpha():messagebox.showerror('仅字母','仅允许字母!')

First of all your if-statement isn't in the enclosed scope of your function, which makes your function empty and it will throw an error. def check(): if not Name.isalpha(): messagebox.showerror('Only letters', 'Only letters are allowed!')

然后,您将不会在函数中解析参数Name,因此在函数Name中将不知道,这将引发错误.

Then you don't parse the argument Name in your function so in your function Name will not known, which will throw an error.

def检查(名称):

然后您就遇到了需要解析参数但又不希望立即调用函数的问题.

Then you have the problem that you need to parse the argument but still don't want you function to be called immediately.

下一步按钮= Button(..,command = lambda:check(name))

它似乎遇到的最后一个问题是,在您创建按钮时将不知道该名称,因为您已经在函数中创建了该变量.但是,只要我看不到其余的内容,我就无法为您解决这个问题.但我认为您可以解决这个问题.

The last issue that it seems to have is that name will not be known at the point you created your button, since you have created that variable in your function. But I can't solve this issue for you as long as I don't see how you build the rest. But I think you can handle this.

这篇关于如何使一个函数识别另一个?Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆