Tkinter Entry widget stays empty in larger programs (Python 2)

2024/10/6 9:18:28

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?


Large 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 nameprint 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()

Small Program


    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 nameprint len(name)print type(name)getname("testuser")
Answer

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

I see two problems:

  • 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.

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

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 functione = 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)

EDIT:

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)
https://en.xdnf.cn/q/119432.html

Related Q&A

How do i implement this python tree linked list code in dart?

Here is the python codedef tree(root_label, branches=[]):for branch in branches:assert is_tree(branch), branches must be treesreturn [root_label] + list(branches)def label(tree):return tree[0]def branc…

How can i find the ips in network in python

How can i find the TCP ips in network with the range(i.e 132.32.0.3 to 132.32.0.44) through python programming and also want to know the which ips are alive and which are dead. please send me.. thanks …

Python Coding (call function / return values)

I am having trouble writing code for this question. I have seen this question asked in a few places but I still cannot figure out the answer from the tips they provided. The question is: Write a progr…

discord.py Mention user by name

I am trying to mention a user by their name in discord.py. My current code is: @bot.command(name=mention) @commands.has_role(OwnerCommands) async def mention(ctx, *, member: discord.Member):memberid = …

Python unexpected EOF while parsing : syntax error

I am trying to do a simple toto history with a dictionary and function however I have this funny syntax error that keeps appearing that states "unexpected EOF while parsing" on the python she…

How can I read part of file and write the rest to another file?

I have multiple large csv file. How can I read part of each file and write 10% of the data/rows to another file?

Encryption/Decryption - Python GCSE [duplicate]

This question already has an answer here:Encryption and Decryption within the alphabet - Python GCSE(1 answer)Closed 8 years ago.I am currently trying to write a program, for school, in order to encryp…

Convert decimal to binary (Python) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Kivy Digital Clock Issues

Im trying to add a digital clock to my Kivy program, it seems to be having trouble.Here is the .py:import kivykivy.require(1.10.0)from kivy.lang import Builder from kivy.uix.screenmanager import Screen…

Read a file name and create a column with it [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…