我的代码为输入框返回一个空值 [英] My code returns an empty value for the entry box

查看:33
本文介绍了我的代码为输入框返回一个空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查以下代码;当按下添加按钮时,框中输入的文本值,必须使用添加功能输入并打印出来
我的旧代码用于返回入口框的 PY_VAR1 值,这是我的旧代码的链接 我的输入框总是返回 PY_VAR1 值!!尽管我使用的是 .get 函数

Please check the following code;when pressing the add button,the value of text entered in the box, must be entered and printed out using the add function
my olde code used to return PY_VAR1 value for the entrybox , here's a link for my old code My entry box always returns PY_VAR1 value!!though I'm using the .get function

from Tkinter import *
root = Tk()
class Addcase:
    def mains(self):
        master = Tk()
        master.wm_title("Add")
        self.content = StringVar()
        self.text = StringVar()
        self.bz = Button(master,text="add",command=self.add)
        self.bz.pack()
        self.l = Label(master,text="ok")
        self.l.pack()
        self.e = Entry(master,textvariable=self.content)
        self.e.pack()
        print self.content.get() + "hello"
        master.mainloop()
    def add(me):
            print "this is the entered text"
            print me.content.get()

def calladd():
    z = Addcase()
    z.mains  


def main():
    calling = Addcase()
    root.wm_title("Hello, me")
    b = Button(root, text="Add a case",command=calling.mains)
    b.pack()
    mainloop()

if __name__ == '__main__':
  main()

推荐答案

这里的问题是您通过调用 Tk() 两次来创建两个根.你不能那样做;如果你这样做,各种奇怪的事情就会发生.

The problem here is that you're creating two roots, by calling Tk() twice. You can't do that; if you do, all kinds of odd things happen.

在这种情况下,发生的奇怪事情是 self.content 最终成为 first 根的一部分,直到second 根完成它的 mainloop.因此,StringVar 内容永远不会更新.

In this case, the odd thing that happened is that self.content ended up being part of the first root, which doesn't get a chance to run until the second root finishes its mainloop. So, the StringVar contents were never getting updated.

(如果你仔细观察,你还会注意到原来的窗口没有正确响应事件.这在不同的平台上有不同的效果——从如果你拖过另一个窗口而不刷新它的显示,到在您关闭第二个窗口之前单击按钮时看起来完全响应但实际上没有做任何事情.)

(If you looked carefully, you'd also notice that the original window didn't respond properly to events. That has a different effect on different platforms—anything from not refreshing its display if you drag another window across it, to looking fully responsive but not actually doing anything when you click the button until after you've closed the second window.)

如果您想创建一个与根在同一主循环中运行的新顶级窗口,您可以使用 顶级.

If you want to create a new top-level window that runs in the same main loop as the root, you do that by using Toplevel.

所以,在Addcase.mains的顶部,不要做master = Tk(),而是做master = Toplevel().然后,在底部,删除 master.mainloop() 调用.

So, at the top of Addcase.mains, instead of doing master = Tk(), do master = Toplevel(). And then, at the bottom, remove that master.mainloop() call.

但是,对于 Tkinter,最好遵循更标准的惯用模式.您有一个类只是用作该 mains 函数的持有者.构造类的实例不会做任何事情;调用该函数可以完成所有操作.将函数编写为函数(在这种情况下 add 将是该函数内的局部函数),或者编写一个在其 __init__ (并且可能从 Toplevel 继承而不是包含它).查看文档中的示例,了解如何执行每种方法.

However, it would really be better to follow a more standard idiomatic pattern for Tkinter. You have a class that's just being used as a holder for that mains function. Constructing an instance of the class doesn't do anything; calling that function does everything. It would be clearer to either write the function as a function (in which case add would be a local function within that function), or write a class that initializes things in its __init__ (and possibly inherits from Toplevel instead of containing it). Look through the examples in the docs for how to do each way.

这篇关于我的代码为输入框返回一个空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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