Tkinter Entry 小部件在较大的程序中保持为空(Python 2) [英] Tkinter Entry widget stays empty in larger programs (Python 2)

查看:44
本文介绍了Tkinter Entry 小部件在较大的程序中保持为空(Python 2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个程序,在点击按钮后,用户会被要求输入其名称,然后程序继续.我坚持让弹出窗口返回已在弹出窗口中输入的文本字符串.起初我以为这是我的代码,但我决定制作第二个程序,它只询问名称,打印它,打印它的长度和类型.在第二个程序中,一切正常.我很难弄清楚为什么它不在第一个(更大的)程序中.我已经阅读过(为什么 Tkinter Entry 的 get 函数什么都不返回?)并且,即使我的 .get()函数出现在我的.mainloop之后,还是不行;在同一个线程中,他们建议使用类,我对此一无所知.如果可能,谁能指出我在更大的程序中遗漏了什么?

I want to make a program where, after clicking on a button, a user gets asked for its name, after which the program continues. I'm stuck on making the popup return the textstring that has been entered in the popup. At first I thought it was my code, but the I decided to make a second program where it just asks the name, prints it, prints it length and its type. In that second program, everything works as it should. I'm having a very hard time figuring out why it doesn't in the first (larger) program. I've already read (Why is Tkinter Entry's get function returning nothing?) and, even though my .get() function occurs after my .mainloop, it still doesn't work; in that same thread they propose using classes, which is something I know absolutely nothing about. If possible can anyone point out what I'm missing in my larger program?

大型程序

from Tkinter import *

root = Tk()
root.title("Ask-name-SUB")

def getname(usertype):
    getname = Tk()
    getname.title("Get name popup")
    def abort():
        getname.destroy()
    name = StringVar()
    c = LabelFrame(getname, text = "Your name:")
    c.pack()
    d = Entry(getname, textvariable=name)
    d.pack(side="right")
    d.bind("<Return>", lambda event: getname.destroy())
    e = Button(getname, text = "Cancel", command=lambda: abort())
    e.pack()

    getname.mainloop()
    name = (name.get())
    print "Print name, its length, its type"
    print name
    print len(name)
    print type(name)

top = Frame(root)
top.pack(side="top")
bottom = Frame(root)
bottom.pack(side="bottom")
def killit():
    root.destroy()

cancel = Button (bottom, text = "Cancel", command=lambda: killit())
cancel.pack()
askname = Button (top, text = "Enter your name", command=lambda: getname("testuser"))
askname.pack()
root.mainloop()

<小时>

小程序

    from Tkinter import *

def getname(usertype):
    getname = Tk()
    getname.title("Get name popup")
    def abort():
        getname.destroy()
    name = StringVar()
    c = LabelFrame(getname, text = "Your name:")
    c.pack()
    d = Entry(getname, textvariable=name)
    d.pack(side="right")
    d.bind("<Return>", lambda event: getname.destroy())
    e = Button(getname, text = "Cancel", command=lambda: abort())
    e.pack()

    getname.mainloop()
    name = (name.get())
    print "Print name, its length, its type"
    print name
    print len(name)
    print type(name)


getname("testuser")

推荐答案

我无法运行您的大型程序 - 可能您的缩进有误.

I can't run your large program - probably you have wrong indentions.

我看到两个问题:

  • 程序应该只有一个 mainloop() - 这可能是你的问题.
  • 我们使用Tk()创建主窗口,使用Toplevel()创建其他窗口.
  • program should have only one mainloop() - and it can be your problem.
  • we use Tk() to create main window, and Toplevel() to create other windows.

此外,您使用名称 getname 作为函数名称和第二个窗口实例,因此非常具有误导性.

Besides you use name getname as function name and as second window instance so it is very misleading.

我创建全局 var_name 来保留名称,然后在函数内部使用它.

I create global var_name to keep name and later I use it inside function.

from Tkinter import *

def get_name(usertype):

    win = Toplevel()
    win.title("Get name popup")

    f = LabelFrame(win, text = "Your name:")
    f.pack()

    # use global `name` which was created outside function
    e = Entry(win, textvariable=var_name)
    e.pack(side="right")
    e.bind("<Return>", lambda event: win.destroy())

    b = Button(win, text = "Cancel", command=win.destroy)
    b.pack()

# --- main --

root = Tk()
root.title("Ask-name-SUB")

# global variable
var_name = StringVar()

b = Button(root, text="Enter your name", command=lambda: get_name("testuser"))
b.pack()

b = Button(root, text="Cancel", command=root.destroy)
b.pack()

root.mainloop()

# --- after --
name = var_name.get()
print "Print name, its length, its type"
print name, len(name), type(name)

<小时>

为了使弹出窗口更通用,您可以使用参数 - 显示文本和结果变量.

To makes popup window more universal you can use arguments - displayed text and variable for result.

def get_value(text, variable):

然后您可以将它用于不同的文本和不同的变量 - 即.姓名或地址.

and then you can use it with different text and different variable - ie. for name or for address.

from Tkinter import *

def get_value(text, variable):

    win = Toplevel()
    win.title("Get value")

    f = LabelFrame(win, text=text)
    f.pack()

    e = Entry(win, textvariable=variable)
    e.pack(side="right")
    e.bind("<Return>", lambda event: win.destroy())

    b = Button(win, text = "Cancel", command=win.destroy)
    b.pack()

# --- main --

root = Tk()
root.title("Ask-name-SUB")

# global variables
var_name = StringVar()
var_address = StringVar()

b = Button(root, text="Enter your name", command=lambda: get_value("Your name:", var_name))
b.pack()

b = Button(root, text="Enter your address", command=lambda: get_value("Your address:", var_address))
b.pack()

b = Button(root, text="Cancel", command=root.destroy)
b.pack()

root.mainloop()

# --- after --

name = var_name.get()
print "Print name, its length, its type"
print name, len(name), type(name)

address = var_address.get()
print "Print address, its length, its type"
print address, len(address), type(address)

这篇关于Tkinter Entry 小部件在较大的程序中保持为空(Python 2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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