'How can I convert a return statement into a printable statement? [closed]

I am making a GFC (Greatest Common Factor) calculator with GUI, but my current code only works with a return statement, and tkinter doesn't accept return to fill a textbox.

Here is a sample of my code

def gproces():
    Gnumber1 = Entry.get(GE1)
    Gnumber2 = Entry.get(GE2)
    Gnumber1 = int(Gnumber1)
    Gnumber2 = int(Gnumber2)

    if Gnumber1 > Gnumber2:
        Gnumber1, Gnumber2 = Gnumber2, Gnumber1

    for x in range (Gnumber1, 0, -1):
        if Gnumber1 % x == 0 and Gnumber2 % x == 0:
            return x

Here is where it's supposed to be used: (To fill GE3)

GE3=Entry(top, bd =5)
GE3.grid(row=3, column=4)
GB=Button(top, text ="Submit", command = gproces).grid(row=4,column=4,)

How do I convert a return statement into something that can be used by Tkinter?



Solution 1:[1]

You can just insert the result into the text box inside gprocess():

def gproces():
    # better cater invalid input
    try:
        Gnumber1 = int(GE1.get())
        Gnumber2 = int(GE2.get())

        x = min(Gnumber1, Gnumber2)
        for x in range(x, 0, -1):
            if Gnumber1%x == 0 and Gnumber2%x == 0:
                break

        # insert result into text box
        GE3.delete(0, 'end')
        GE3.insert('end', x)
    except ValueError:
        print('Invalid number input')

Note that there is math.gcd() to find the GCF.

Solution 2:[2]

Ok so if I understand this correctly you want the result to be displayed in a textbox.

This code should work:

T.delete('1.0', tk.END)  # deletes all previous data
T.insert('1.0', str(x))  # inserts new data

Where T is your textbox.

If you want to fill the Textbox in a loop you can simply run T.insert('1.0', str(x)) instead of using return at the end of your function, or you can use both return and T.insert, but don't forget to clear the textbox at the begining of the function with T.delete.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 acw1668
Solution 2 Mario