0

Is there an easy way to handle input from an entry box, in my project I am working on I have to call functions twice which ends up making a large mess of everything.
Like if I call the

import tkMessageBox
from Tkinter import *

def yes_no_ask(answer):
   guidisplay.actiondisplay.configure(state=NORMAL)
   guidisplay.actiondisplay.insert(INSERT, "\n" + question)
   guidisplay.actiondisplay.configure(state=DISABLED)
   # What I want to happen is:
   # if input_from_entry_box == "yes":
   # return 1
   # elif input_from_entry_box == "no"
   # return 0
   # But I can't call this again from the input_handler.


   

class GUIdisplay:
   
   def __init__(self, root):
      # Set-up Menu bar.
      self.menubar = Menu(root)
      # ------FILE MENU--------
      self.filemenu = Menu(self.menubar, tearoff=0)
      self.filemenu.add_command(label="New", command=self.newgame)
      self.filemenu.add_separator()
      self.filemenu.add_command(label="Exit", command=self.exithandler)
      self.menubar.add_cascade(label="File", menu=self.filemenu)
      # ------EDIT MENU--------
      self.editmenu = Menu(self.menubar, tearoff=0)
      self.menubar.add_cascade(label="Edit", menu=self.editmenu)
      # ------HELP MENU--------
      self.helpmenu = Menu(self.menubar, tearoff=0)
      self.menubar.add_cascade(label="Help", menu=self.helpmenu)
      root.config(menu=self.menubar)

      # Make the frame that holds the action text display.
      self.actiondisplayFrame = Frame(root)
      self.actiondisplayFrame.pack()

      # Make the frame that holds the user input.
      self.userinputFrame = LabelFrame(root, text="User input")
      self.userinputFrame.pack()

      # Make display area.
      self.actiondisplay = Text(self.actiondisplayFrame)
      self.actiondisplay.rowconfigure(0, weight=1)
      self.actiondisplay.columnconfigure(0, weight=1)
      self.actiondisplay.configure(wrap=WORD)
      self.actiondisplay.configure(font=("fixedsys", 8))
      self.actiondisplayScrollbar = Scrollbar(self.actiondisplayFrame, orient=VERTICAL, command=self.actiondisplay.yview)
      self.actiondisplay.configure(yscrollcommand = self.actiondisplayScrollbar.set)
      self.actiondisplay.insert(INSERT, "Welcome to <insert cool name here>. \nYou are about to enter a world of <insert cool description of name here>. \nIf this is your first time select New from the File menu or \n<add other options here>")
      self.actiondisplay.configure(state=DISABLED)
      self.actiondisplayScrollbar.pack(side = RIGHT,  fill=Y)
      self.actiondisplay.pack()

      # User input display area.
      self.userinput = Entry(self.userinputFrame)
      self.userinput.bind("<Return>", self.inputhandler_wrapper)
      self.userinput.rowconfigure(0, weight=1)
      self.userinput.columnconfigure(0, weight=1)
      self.userinput.configure(width = 50)
      self.userinput.configure(bd = 3)
      self.inputenterbutton = Button(self.userinputFrame, text="Enter", padx = 5, command=self.inputhandler)
      self.inputenterbutton.bind("<Return>", self.inputhandler_wrapper)
      self.inputclearbutton = Button(self.userinputFrame, text="Clear", padx = 4, command=self.clearuserinput)
      self.inputclearbutton.bind("<Return>", self.clearuserinput_wrapper)
      self.inputclearbutton.pack(side = RIGHT)
      self.userinput.pack(side = LEFT)
      self.inputenterbutton.pack(side = LEFT)

   def look_command(self):
      room_description = "A plain white room."
      self.actiondisplay.configure(state=NORMAL)
      self.actiondisplay.insert(INSERT, "\n" + room_description)
      self.actiondisplay.configure(state=DISABLED)

   def exithandler(self):
      root.destroy() 

   def newgame(self):
      # Creates a new player
      print "New game function activated"
      self.creating_new_game = 1
      self.player_adventurer = adventurer()
   
   def clearuserinput(self):
      # Clears all text from the Input box.
      self.lengthofinput = len(self.userinput.get())
      self.userinput.delete(0, last=self.lengthofinput)
      
   def clearuserinput_wrapper(self, event):
      # This is just a function to handle event binding.
      self.clearuserinput()

   def inputhandler(self):
      # Will sort and execute the text sent from the input box.
      # This is the area I would like to change.
      user_input = self.userinput.get()
      if self.creating_new_game == 1:
         self.player_adventurer.set_up_stats(user_input)
      else:
         self.clearuserinput()
         self.actiondisplay.configure(state=NORMAL)
         self.actiondisplay.insert(INSERT, "\n" + user_input)
         self.actiondisplay.configure(state=DISABLED)
         self.actiondisplay.yview ( MOVETO, 1.0 )
         user_input = user_input.lower()
         if user_input == "look":
            self.look_command()

   def inputhandler_wrapper(self, event):
      # This is just a function to handle event binding.
      self.inputhandler()

class adventurer():
   def __init__(self):
      self.set_up_stage = 0
      guidisplay.actiondisplay.configure(state=NORMAL)
      guidisplay.actiondisplay.insert(INSERT, "\n\nWhat would you like to be called?")
      guidisplay.actiondisplay.configure(state=DISABLED)
   def set_up_stats(self, user_input):
      if self.set_up_stage == 0:
         self.set_name(user_input)
         
   def set_name(self, user_input):
      self.questionstr = "Are you sure you want to be called " + user_input + "?"
      self.yesorno = yes_no_question(self.questionstr)
      self.name = user_input
      

      
root = Tk()
guidisplay = GUIdisplay(root)
root.mainloop()

Thanks heaps.

PS. I am new to GUI so if I have done anything else wrong please tell me.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by woooee
1

You use a StringVar and include the function in the class. Here is a simple example. Note that the same StringVar is used for the second label and the entry box. Take a look at this page http://infohost.nmt.edu/tcc/help/pubs/tkinter/control-variables.html

import Tkinter

class EntryTest:
   def __init__(self):
      self.top = Tkinter.Tk()

      self.str_1 = Tkinter.StringVar()
      label_lit = Tkinter.StringVar()

      label_1 = Tkinter.Label(self.top, textvariable = label_lit )
      label_1.pack()
      label_lit.set( "Test of Label")
    
      label_2 = Tkinter.Label(self.top, textvariable = self.str_1 )
      label_2.pack()

      entry_1 = Tkinter.Entry(self.top, textvariable=self.str_1)
      entry_1.pack()
      self.str_1.set( "Entry Initial Value" )

      cont = Tkinter.Button(self.top, text='PRINT CONTENTS',
             command=self.getit, bg='blue', fg='white' )
      cont.pack(fill=Tkinter.X, expand=1)

      exit=  Tkinter.Button(self.top, text='EXIT',
             command=self.top.quit, bg='red', fg='white' )
      exit.pack(fill=Tkinter.X, expand=1)

      entry_1.focus_set()
      self.top.mainloop()


   ##-----------------------------------------------------------------
   def getit(self) :
      print "getit: variable passed is", self.str_1.get()

##===============================================================

if "__main__" == __name__  :
   ET=EntryTest()
Votes + Comments
right on
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.