python - Tkinter: Set variable to string input -


i've been working python , tkinter little while now, , have decided make text based game. know how functions in python, want have actual window, not console. i've figured out how make window , display string of text... want able type , show text.

from tkinter import *  w=tk()  textvar="welcome, pit of insanley intense things... , stuff!!!"  def key(event):     print event.char  t=text(w) t.insert(insert,textvar) t.configure(state=disabled) t.bind("<key>",key) t.pack()  w.mainloop() 

this code makes simple window , displays value of string variable. also, got print key press in console, want able add these characters string, can see type. kind of essential part of text based game :)

i'd appreciate anyone's this. thanks

the code below shows how use entry text input user, instead of trying handle key events manually.

it shows 2 different ways can have user trigger you—hitting enter/return key while entry has focus call enter function, while clicking button call click function. there kinds of other things can trigger off—each change contents, each keypress, entry losing focus, … these 2 should enough show idea.

finally, shows how use contents of entry modify text, instead of printing console. trick here read-only text read-only insert/etc. methods, have temporarily switch normal. (the text docs explain third of way down page.)

from tkinter import *  w=tk()  textvar="welcome, pit of insanley intense things... , stuff!!!"  t=text(w) t.insert(insert,textvar) t.configure(state=disabled) t.pack()  def do_command(command):     t.configure(state=normal)     t.insert(insert, '\nyou said, "{}"'.format(command))     t.configure(state=disabled)  e=entry() e.pack(side=bottom)  def enter(event):     do_command(e.get())  e.bind('<keyrelease-return>', enter)  def click():     do_command(e.get())  b=button(text='doit!', command=click) b.pack(side=bottom)  w.mainloop() 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -