我的输入框总是返回 PY_VAR1 值!!尽管我使用的是 .get 函数 [英] My entry box always returns PY_VAR1 value!!though I'm using the .get function

查看:21
本文介绍了我的输入框总是返回 PY_VAR1 值!!尽管我使用的是 .get 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看一下我的代码,这真的很简单,我需要从输入框中获取值并在我的程序中使用它,当按下添加按钮时我打印它,它一直给我这个值 PY_VAR1?!!想知道我做错了什么!

please take a look at my code, it's really simple I need to take the value from the entry box and use it in my program and when pressing the add button I print it ,it keeps giving me this value PY_VAR1?!! wondering what I'm doing wrong!

这是我的问题的可运行代码的链接我的代码为输入框返回一个空值

here's the link for t runnable code for my question My code returns an empty value for the entry box

from Tkinter import *
    root = Tk()
    content = StringVar()
    text = StringVar()
    class addcase:
        def mains(self):
            master = Tk()
            master.wm_title("Add")
            bz = Button(master,text="add",command=self.add)
            bz.pack()
            l = Label(master,text="ok")
            l.pack()
            e = Entry(master,textvariable=content)
            e.pack()
            e.focus_set()
            content.set("default value")
            text = content.get()
            master.mainloop()
        def add(me):
                print content.get()
                print text

推荐答案

既然你还没有真正给我们一个可运行的例子,这真的只是一个猜测,但是……

Since you haven't actually given us a runnable example, this is really no more than a guess, but…

当您修复代码使其实际运行时,然后单击按钮,它会打印:

When you fix the code so it actually runs, then click the button, it prints this:

default value
PY_VAR1

如果你看一下代码,它是这样做的:

If you look at the code, it does this:

    def add(me):
            print content.get()
            print text

第一行在您使用 contents.set('default value') 初始化的 StringVar 上调用 get,并且从不再次修改,当然打印出default value.据我所知,您对此并不感到惊讶.

The first line calls get on a StringVar that you've initialized with contents.set('default value'), and never modified again, so of course it prints out default value. As far as I can tell, you aren't surprised by this.

第二行不会调用名为 textStringVar 上的任何内容.只需 print 一个 StringVar,而不是调用 get() 并打印结果,导致它打印变量的 Tk 名称.您还在 addcase 中定义了同名的局部变量这一事实无关紧要.据我所知,这是让您感到惊讶的地方.但你不应该.add 函数看不到您在完全不相关的函数中创建的任何局部变量.

This second line doesn't call anything on the StringVar named text. Just printing a StringVar, instead of calling get() on it and printing the result, causes it to print the Tk name of the variable. The fact that you've also defined a local variable of the same name within addcase is irrelevant. As far as I can tell, this is what you're surprised by. But you shouldn't be. The add function can't see any local variables you've created in a completely unrelated function.

如果您希望在类的同一实例的不同方法之间共享值,请将它们存储在实例属性中,而不是局部变量中.

If you want a value to be shared between different methods of the same instance of a class, store them in an instance attribute, rather than a local variable.

但是,更简单地说,如果您只是避免在完全独立的范围内为完全不同的变量重用相同的名称,您可能会避免混淆自己,并且很明显发生了什么.

But, more simply, if you just avoided reusing the same name for completely different variables in completely independent scopes, you would probably avoid confusing yourself, and it would be obvious what was going on.

这篇关于我的输入框总是返回 PY_VAR1 值!!尽管我使用的是 .get 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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