Hello, I'm pretty new to Python, and I've been making simple projects to practice in. The issue I've run into though is I'm creating a GUI (Using Tkinter with Python 2.6) to take a text input and bring out "Name". What I want to do though is keep that "Name" variable through out, as if I assigned a variable to a raw_input. I just can't figure out what to use to do the same thing. Can anyone point me in the right direction?
Below is what I have so far, the creation of the GUI and an initial print statement. Thanks in advance.

#!usr/bin/python
from Tkinter import *

def Naming():
name = text.get()
print name
root.destroy()

root = Tk()
root.geometry=("100x100+100+50")

text = Entry(root, bg="white")
text.pack()
text.focus_force()

nameButton = Button(root, text="Accept", command=Naming)
nameButton.pack(side=BOTTOM, anchor=S)

root.mainloop()

You want to use a class. The variable will be visible and accessible as long as the class exists.

from Tkinter import *

class Demo:
   def __init__(self, top):
      self.name=""
      frame_e = Frame(top)
      frame_e.pack()
      self.t_name = StringVar()

      text = Entry(frame_e, textvariable=self.t_name, bg="white")
      text.pack()
      text.focus_force()

      nameButton = Button(frame_e, text="Accept", command=self.Naming)
      nameButton.pack(side=BOTTOM, anchor=S)


   def Naming(self):
      self.name = self.t_name.get()
      print self.name
      root.destroy()

root = Tk()
root.geometry=("100x100+100+50")

D=Demo(root)

root.mainloop()

print "name retrieved was", D.name

You can use this approach to get an input dialog without the Tkinter window:

# use Tkinter's dialogs to ask for input
# dialogs are askfloat, askinteger, and askstring

import Tkinter as tk
from tkSimpleDialog import askstring

root = tk.Tk()
# show askstring dialog without the Tkinter window
root.withdraw()
name = askstring("Name", "Enter your name")

print(name)

With Python 30 use:

# use Tkinter's dialogs to ask for input
# dialogs are askfloat, askinteger, and askstring
# mofified for Python30

import tkinter as tk
from tkinter.simpledialog import askstring

root = tk.Tk()
# show askstring dialog without the Tkinter window
root.withdraw()
name = askstring("Name", "Enter your name")

print(name)

Thank you for the help woooee. I do have a couple questions since a few of the things you have shown me are new to me. What exactly is StringVar()? Also how does t_name.get() know to grab the entered text from the Entry? Is that what the textvariable is for? And finally what's the difference between textvariable and StringVar()?

Tkinter is just a wrapper around TCL/Tk, which is also a scripting language, so it defines it's own variables. So
self.t_name = StringVar()
declares the variable self.t_name as a TCL/Tk string
text = Entry(frame_e, textvariable=self.t_name, bg="white")
says that name of the variable containing the text/data, for the widget named text, is "self.t_name".
self.name = self.t_name.get()
converts a Tk string variable to a python string variable.
Here are links that between them answer pretty much any question that a normal user might have.
## Control Variables at N.M. Tech
http://infohost.nmt.edu/tcc/help/pubs/tkinter/control-variables.html

## Fredrik Lundh's page (effbot.org)
http://www.pythonware.com/library/tkinter/introduction/index.htm

## New Mexico Tech
http://infohost.nmt.edu/tcc/help/pubs/tkinter/

Thank you very much. I have learned a good deal just from this post alone. I'm sure any future questions I have will be just as educational thanks to this site. Thank you for the help.

hello i just started doing a computer science course and i was told to make a recipemaker which i'll just compeleted but am finding it hard to store my ingedients into my listbox. help please

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.