Tkinter - How to limit the space used by a Text?

Viewed 228

I'm trying to create a factorial calculator GUI. The program works fine, but the problem I'm having is that when there are too many numbers coming in from the output, the screen automatically increases in width. I've tried using tk.Text to create a limit to the size of the textbox and so the text continues to the next row when the columns are filled.

But when I had to input text in to the tk.Text it didn't work since the variable I used is being processed in the function that gets called when the button is pressed. I have tried googling this problem but I couldn't find anything, I did find some people explaining how to use variables that get created/processed inside of a function, but that didn't work so I think I have done something wrong in my code.

Note: I am using lambda to call my function (not sure if this is important or not).

TLDR: Text gets too long when too much information is outputted. tk.Text didn't work for me since I couldn't figure out how to use the variable that is created/processed inside of a function that is only called when the button is pressed.

Here is my entire code: https://pastebin.com/1MkdRjVE

Code for my function:

def start_calc():
    output_array = ["placehold"]
    start_text.set("Loading...")
    i = 1
    global e1
    global e2
    output_array.clear()
    string = e1.get()
    string2 = e2.get()
    integr = int(string)
    integr2 = int(string2)
    if string == "":
        error_message.set("Please enter correct numbers.")
    elif string2 == "":
        error_message.set("Please enter correct numbers.")
    else:
        while integr2 >= i:
            calc = integr ** i
            calcstr = (str(calc))
            output_array.append(calcstr)
            i += 1

    start_text.set("Start!")
    output_array_str = (', '.join(output_array))
    output_msg.set("Output: " + output_array_str)
    print(output_array_str) #This is just so I know if it's working or not in the terminal

Code for my output:

output_msg = tk.StringVar()
output_text = tk.Label(root, textvariable=output_msg, font="Raleway")
output_msg.set("Output: ")
output_text.grid(columnspan=3, column=0, row=14)
1 Answers

I think this is what you are looking for:

#Imports
import tkinter as tk

#Variables
root = tk.Tk()


#Tkinter GUI setup basic
canvas = tk.Canvas(root, width= 400, height=400)
canvas.grid(columnspan=3, rowspan=120)

#Title
text = tk.Label(root, text="Calculating factorials", font="Raleway")
text.grid(column=1, row=1)

#Function
def start_calc():
    output_array = ["", ""]
    start_text.set("Loading...")
    i = 1
    global e1
    global e2
    output_array.clear()
    string = e1.get() 
    string2 = e2.get()
    integr = int(string)
    integr2 = int(string2)
    if string == "":
        error_message.set("Please enter correct numbers.")
    elif string2 == "":
        error_message.set("Please enter correct numbers.")
    else:
        while integr2 >= i:
            calc = integr ** i
            calcstr = (str(calc))
            output_array.append(calcstr)
            i += 1   
    start_text.set("Start!")
    output_array_str = (', '.join(output_array))
    # Change the output
    output_text.config(state="normal")
    # delete last output:
    output_text.delete("0.0", "end")
    # insert new output:
    output_text.insert("end", output_array_str)
    output_text.config(state="disabled")
    print(output_array_str) #This is just so I know if it's working or not in the terminal
    
    
#input
tk.Label(root, text="Number :").grid(row=10)
tk.Label(root, text="Factorial :").grid(row=11)
e1 = tk.Entry(root)
e2 = tk.Entry(root)
e1.grid(row=10, column=1)
e2.grid(row=11, column=1)

#Error message if the input is invalid
error_message = tk.StringVar()
error_text = tk.Label(root, textvariable=error_message, font="Raleway")
error_message.set(" ")
error_text.grid(column=1, row=12)

#Startbutton
start_text = tk.StringVar()
start_btn = tk.Button(root, textvariable=start_text, command=start_calc, font="Raleway", bg="#20bebe", fg="white", height=2, width=15)
start_text.set("Start!")
start_btn.grid(column=1, row=13, pady=10)

#output
output_text = tk.Text(root, height=1, width=20, wrap="none", font="Raleway")
output_text.insert("end", "Output")
output_text.config(state="disabled")
output_text.grid(columnspan=3, column=0, row=14, sticky="news")

#Adding a scrollbar
scrollbar = tk.Scrollbar(root, orient="horizontal", command=output_text.xview)
scrollbar.grid(columnspan=3, column=0, row=15, sticky="news")
output_text.config(xscrollcommand=scrollbar.set)

#disclaimer message
disclaimer_text = tk.Label(root, text="Disclaimer: The factorials will be printed from 1 to the number you entered.")
disclaimer_text.grid(columnspan=3, column=0, row=110)

root.mainloop()

I used a <tkinter.Text> widget with wrap="none", height=1 and width=20 to make the output box. I disabled the entry so that the user can't change the results but can still copy it.

Related