Get Strings from Entry Boxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 360monkey
    New Member
    • Feb 2010
    • 17

    Get Strings from Entry Boxes

    Hey Bytes Community!

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

    Code:
    from Tkinter import *
    import random
    
    class App:
    	
    	def __init__(self, master):
    		frame = Frame(master)
    		frame.pack()
    		menu = Menu(root)
    		root.config(menu=menu)
    		filemenu = Menu(menu)
    		menu.add_cascade(label='File', menu=filemenu)
    		filemenu.add_command(label='Practice', command=self.namesetare)
    		filemenu.add_command(label='Story Mode', command=self.namesetadv)
    		filemenu.add_command(label='Quit', command=frame.quit)
    		infomenu = Menu(menu)
    		menu.add_cascade(label='About...', menu=infomenu)
    		infomenu.add_command(label='About BattlerIII', command=self.about)
    
    	def namesetare(self):
    		nsa = Toplevel()
    		self.pone = Label(nsa, text='Player One:').grid(row=0)
    		[U]self.e1 = Entry(nsa).grid(row=0, column=1)[/U]
    		self.ptwo = Label(nsa, text='Player Two:').grid(row=1)
    		[U]self.e2 = Entry(nsa).grid(row=1, column=1)[/U]
    		self.next = Button(nsa, text='Next', command=self.arena)
    		self.next.grid(row=2, column=1)
    		return self.e1
    		
    	def arena(self):
    		areone = Toplevel()
    		[U]hmn1 = str(self.e1.get())
    		hmn2 = str(self.e2.get())[/U]
    		self.text1 = Label(areone, text=hmn1)
    		self.text1.pack()
    		aretwo = Toplevel()
    		self.text2 = Label(aretwo, text=hmn2)
    		self.text2.pack()
    		self.attack1 = Button(areone, text='ATTACK', fg='red', command=self.atk1)
    		self.attack1.pack(side=LEFT)
    		self.attack2 = Button(aretwo, text='ATTACK', fg='red', command=self.atk2)
    		self.attack2.pack(side=LEFT)
    			
    root = Tk()
    root.title('BattlerIII')
    app = App(None)
    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!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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.
    Code:
        def namesetare(self):
            nsa = Toplevel()
            self.pone = Label(nsa, text='Player One:').grid(row=0)
            self.v1 = StringVar()
            self.e1 = Entry(nsa, textvariable=self.v1)
            self.e1.grid(row=0, column=1)
    
            self.ptwo = Label(nsa, text='Player Two:').grid(row=1)
            self.v2 = StringVar()
            self.e2 = Entry(nsa, textvariable=self.v2)
            self.e2.grid(row=1, column=1)
    
            self.next = Button(nsa, text='Next', command=self.arena)
            self.next.grid(row=2, column=1)
            self.next1 = Button(nsa, text='Quit', command=nsa.destroy)
            self.next1.grid(row=2, column=2)
            return self.e1
    Set the value of the labels in the arena method to the same values used in the Entry widgets. Example:
    Code:
            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.
    Last edited by bvdet; Feb 12 '10, 11:16 PM. Reason: fixed code

    Comment

    • 360monkey
      New Member
      • Feb 2010
      • 17

      #3
      Thank You! Your answer has helped me a lot!

      Comment

      Working...