GUI. User input. Multi-line text box

user2978216 Source

A simple GUI where a user can input multiline text is needed. Here is my code how can I get the value from the form? Do I have to manually create buttons too? I like the simplicity of gooey module but it seems its unable to make multiline textbox? What would be the best way to get the subject done?

import tkinter as tk
root=tk.Tk()
text=tk.Text(root)
text.pack()
root.mainloop()
pythonwindowspython-3.xuser-interfacetkinter

Answers

answered 5 days ago dsgdfg #1

like this (Python2.7):

from Tkinter import *
root=Tk()
text=Text(root)
text.pack()
gui = {}
gui["text"] = text
def keyUp(e):
    print e.keycode
    oldText = gui["text"].get(1.0,END)
    oldText = oldText[:-1] if (oldText[-1] == u"\n") else oldText
    if e.keycode in (36,104) :
        gui["text"].delete(1.0,END)
        if  ord(oldText[-1]) != 10 :
            newText = oldText + "\n"
            gui["text"].insert("1.0",newText)
        else :
            gui["text"].insert("1.0",oldText)
        gui["text"].update()


gui["text"].insert(1.0,u"Re hello\nWorld\n")

gui["text"].bind("<KeyRelease>", lambda  event: keyUp(event))
root.mainloop()

Suppress all new line characters on the keyboard.(i got 36(alpha),104(numlock)). Some operating systems may be able to add lines to the writing box(my big Enter Key). Ignore if the last character is a new line character oldText = oldText[:-1] if (oldText[-1] == u"\n") else oldText.Without forgetting, you cannot add blank lines!

comments powered by Disqus