Question

I am creating a Python factorial program, and I have set it up with tkinter to print the number you input to the input box into the Python shell, but how do I instead print it into a separate text box? Here is my code: from 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()
Was it helpful?

Solution

You use the insert method of the text widget:

def callback():
    n = int(e.get())
    result = factorial(n)
    t.insert("1.0", str(result))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top