galfar
galfar

Reputation: 36

Weird thing with tkinter Label, or list

hello I need help!

I tried to code a script what will help me with some math, but when I coded something weird happened. Something that never happened to me. My current code is:

import tkinter as tk


my_list = [] 


root = tk.Tk()
root.title('My own calculator')


entry1 = tk.Entry(root)
entry1.pack()


Label1 = tk.Label(root)
Label1.pack()

def Calculate(event):
    n = entry1.get()
    my_list.clear()
    try:
        Label1.config(text="")
        print("The divisors of the number are:")
        for i in range(1,int(n)+1):
            if(int(n)%i==0):
                print(i)
                
                my_list.append("\n" + str(i))
        print(*my_list)
        Label1.config(text=my_list)
    except:
        print("OOOPS")

Calculate_button = tk.Button(root,text="Calculate Divisors", command=Calculate)
Calculate_button.pack()


root.bind('<Return>', Calculate)


root.mainloop()

the result for every number(I used 21) shows this: Screen shot

Please help me.

Upvotes: 0

Views: 40

Answers (1)

acw1668
acw1668

Reputation: 46688

You should just append str(i) to my_list:

my_list.append(str(i))

And to better show the list in the label, join the list items with ', ':

Label1.config(text=', '.join(my_list))

Upvotes: 1

Related Questions