473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Get Strings from Entry Boxes

360monkey
Hey Bytes Community!

I am learning Tkinter GUI and have come across a problem.

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import random
  3.  
  4. class App:
  5.  
  6.     def __init__(self, master):
  7.         frame = Frame(master)
  8.         frame.pack()
  9.         menu = Menu(root)
  10.         root.config(menu=menu)
  11.         filemenu = Menu(menu)
  12.         menu.add_cascade(label='File', menu=filemenu)
  13.         filemenu.add_command(label='Practice', command=self.namesetare)
  14.         filemenu.add_command(label='Story Mode', command=self.namesetadv)
  15.         filemenu.add_command(label='Quit', command=frame.quit)
  16.         infomenu = Menu(menu)
  17.         menu.add_cascade(label='About...', menu=infomenu)
  18.         infomenu.add_command(label='About BattlerIII', command=self.about)
  19.  
  20.     def namesetare(self):
  21.         nsa = Toplevel()
  22.         self.pone = Label(nsa, text='Player One:').grid(row=0)
  23.         self.e1 = Entry(nsa).grid(row=0, column=1)
  24.         self.ptwo = Label(nsa, text='Player Two:').grid(row=1)
  25.         self.e2 = Entry(nsa).grid(row=1, column=1)
  26.         self.next = Button(nsa, text='Next', command=self.arena)
  27.         self.next.grid(row=2, column=1)
  28.         return self.e1
  29.  
  30.     def arena(self):
  31.         areone = Toplevel()
  32.         hmn1 = str(self.e1.get())
  33.         hmn2 = str(self.e2.get())
  34.         self.text1 = Label(areone, text=hmn1)
  35.         self.text1.pack()
  36.         aretwo = Toplevel()
  37.         self.text2 = Label(aretwo, text=hmn2)
  38.         self.text2.pack()
  39.         self.attack1 = Button(areone, text='ATTACK', fg='red', command=self.atk1)
  40.         self.attack1.pack(side=LEFT)
  41.         self.attack2 = Button(aretwo, text='ATTACK', fg='red', command=self.atk2)
  42.         self.attack2.pack(side=LEFT)
  43.  
  44. root = Tk()
  45. root.title('BattlerIII')
  46. app = App(None)
  47. root.mainloop()
what i want to do (the important parts are underlined), is be able to recall the strings inputted in the GUI.
so if i said:

print(hmn1)
it would say what was put into the entry boxes.

similarly, a = input('')
print(a)

any help would be appreciated!
Thanks!
Feb 12 '10 #1
2 2280
bvdet
2,851 Expert Mod 2GB
Save the entry widgets to self.e1 and self.e2. Apply grid() to the widget objects separately. In Entry(), set textvariable to a StringVar object. I modified method namesetare() below.
Expand|Select|Wrap|Line Numbers
  1.     def namesetare(self):
  2.         nsa = Toplevel()
  3.         self.pone = Label(nsa, text='Player One:').grid(row=0)
  4.         self.v1 = StringVar()
  5.         self.e1 = Entry(nsa, textvariable=self.v1)
  6.         self.e1.grid(row=0, column=1)
  7.  
  8.         self.ptwo = Label(nsa, text='Player Two:').grid(row=1)
  9.         self.v2 = StringVar()
  10.         self.e2 = Entry(nsa, textvariable=self.v2)
  11.         self.e2.grid(row=1, column=1)
  12.  
  13.         self.next = Button(nsa, text='Next', command=self.arena)
  14.         self.next.grid(row=2, column=1)
  15.         self.next1 = Button(nsa, text='Quit', command=nsa.destroy)
  16.         self.next1.grid(row=2, column=2)
  17.         return self.e1
Set the value of the labels in the arena method to the same values used in the Entry widgets. Example:
Expand|Select|Wrap|Line Numbers
  1.         self.text1 = Label(areone, textvariable=self.v1)
The text in the label widgets is now linked to the text in the Entry widgets because they share the same values.
Feb 12 '10 #2
Thank You! Your answer has helped me a lot!
Feb 12 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
2
by: Ian Vincent | last post by:
I am hoping someone may be able to help. I am using Python and TKinter to create a GUI program that will eventually create an XML file for a project I am working on. Now, the XML file contents...
1
by: edworboys | last post by:
I have a form (Prospect) with a subform (Document). The result I am looking for is this: The user selects a prospect which is made up of Prospect Name, Country, Company and Prospect Type. They...
1
by: Shannan Casteel via AccessMonster.com | last post by:
I want to have a form with 40 PartNumber text boxes, 40 PartDescription text boxes, and 40 PartPrice text boxes. The name of each would vary of course, but I want to be able to enter the part...
7
by: Mike | last post by:
List, I call this a "Parsing Problem", but it could be called formatting or regular expressions as well. I have a set of data that was formerly processed on an OS390 (hence a lot of...
4
by: sanou | last post by:
Hi No doubt this question, or a similar version of it, has been asked before. But, I was looking around the forums and i couldn't find anything. I'm using VC++ to create a simple calculator...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
4
by: Andrew Meador - ASCPA, MCSE, MCP+I, Network+, A+ | last post by:
I have created a report. This report needs to display records between two dates entered by the user. I put two text boxes on the report so I can enter the start and end date - I set them to use an...
8
by: Wingot | last post by:
Hey, I have a program I am trying to write using Visual C#, SQL Server 2005/2008, and Visual Studio 2008, and one part of it includes a Schema called Client. Inside this schema, three tables...
2
by: Semajthewise | last post by:
Ok guys, This is a quick overview of what I'm trying to do. 2 textboxes. textbox1 has a fraction entered by the user into it in one of these 2 ways 1(space)3/4 or simply 3/4 Now what this code is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.