繁体   English   中英

tkinter Label 或列表的奇怪事情

[英]Weird thing with tkinter Label, or list

你好我需要帮助!

我试图编写一个脚本来帮助我进行一些数学运算,但是当我编写代码时发生了一些奇怪的事情。 从来没有发生在我身上的事情。 我目前的代码是:

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()

每个数字的结果(我使用 21)显示:屏幕截图

请帮我。

你应该只是 append str(i)my_list

my_list.append(str(i))

为了更好地显示 label 中的列表,请使用', '加入列表项:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM