GUI。用户输入。多行文本框

问题描述 投票:-2回答:1

需要一个用户可以输入多行文本的简单GUI。这是我的代码如何从表单中获取值?我是否还必须手动创建按钮?我喜欢gooey模块的简单性,但它似乎无法制作多行文本框?完成主题的最佳方法是什么?

import tkinter as tk
root=tk.Tk()
text=tk.Text(root)
text.pack()
root.mainloop()
python windows python-3.x user-interface tkinter
1个回答
0
投票

像这样(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()

禁止键盘上的所有新行字符。(我得到36(alpha),104(numlock))。某些操作系统可能能够在写入框中添加行(我的大Enter键)。忽略如果最后一个字符是新行字符oldText = oldText[:-1] if (oldText[-1] == u"\n") else oldText。没有忘记,你不能添加空行!

© www.soinside.com 2019 - 2024. All rights reserved.