Python GUI - 打印到文本框而不是Shell Py 3.3

时间:2014-01-26 16:03:22

标签: python user-interface python-3.x tkinter tk

我正在创建一个Python因子程序,我已经使用tkinter将其设置为将您输入到输入框的数字打印到Python shell中,但是如何将其打印到单独的文本框中呢?这是我的代码:来自tkinter import *

def factorial(n):
    first = True
    for number in range(1, n+1):
        if first:
            answer = number
            first = False
        else:
            answer = answer * number
    return answer

    def callback():
    n = int(e.get())
    print(factorial(n))

    window = Tk()

    e = Entry(window)
    e.pack()

    t = Text()
    t.pack()

    e.focus_set()
    b = Button(window, text="get", width=10, command=callback)
    b.pack()

    mainloop()

1 个答案:

答案 0 :(得分:0)

您使用文本小部件的insert方法:

def callback():
    n = int(e.get())
    result = factorial(n)
    t.insert("1.0", str(result))