python tkinter从命令中使用的函数返回值 [英] python tkinter return value from function used in command

查看:96
本文介绍了python tkinter从命令中使用的函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得返回值 A 到 C?顺便说一下,我没有使用类.

how do i get the return value A to C? I am not using class by the way.

def button:
    mylabel = Label(myGui, text = "hi").grid(row = 0, column = 0)
    A = B.get()
    return A

B = StringVar()
C = ""
myentry = Entry(myGui, textvariable = B).grid(row = 1, column = 0)
Submit = Button(myGui, text = "Submit", command = button).grid(row = 1, column = 1)

推荐答案

简短的回答:你不能.回调不能返回任何东西,因为它无处可返回——除了事件循环,它不会对返回值做任何事情.

Short answer: you cannot. Callbacks can't return anything because there's nowhere to return it to -- except the event loop, which doesn't do anything with return values.

在基于事件的应用程序中,您通常要做的是在类上设置属性.或者,如果您是初学者,您可以设置一个全局变量.对于必须随时间维护的真实代码,使用全局变量并不是一个好主意,但它可以用于实验.

In an event based application, what you typically will do is set an attribute on a class. Or, if you're a beginner, you can set a global variable. Using a global variable isn't a good idea for real code that has to be maintained over time but it's OK for experimentation.

因此,例如,由于 C 在您的示例中似乎是一个全局变量,您可以执行以下操作:

So, for example, since C appears to be a global variable in your example, you would do something like:

def button():
    global C
    mylabel = Label(myGui, text = "hi").grid(row = 0, column = 0)
    A = B.get()
    C = A

这篇关于python tkinter从命令中使用的函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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