简体   繁体   English

如何创建动态标签并在python 3 tkinter / ttk中填充它们

[英]How to create dynamic Labels and populate them in python 3 tkinter/ttk

I am trying to display the number of logical CPU's in a machine. 我正在尝试显示计算机中逻辑CPU的数量。 Since they are of varying nature, how can I achieve this dynamically? 由于它们具有不同的性质,我该如何动态地做到这一点?

ie My system can have just 1 logical cpu(can be found in proc/stat/ file) where as someone else might have 4. I want to create Labels for each Logical Cpu present on a particular system and display it's details on my GUI 即我的系统只能有1个逻辑cpu(可以在proc / stat /文件中找到),而其他人可能有4个。我想为特定系统上存在的每个逻辑Cpu创建标签,并在我的GUI上显示它的详细信息

You create them the way you create any other object: 您可以通过创建其他对象的方式来创建它们:

for i in range(number_of_cpus):
    label = tk.Label(parent, text="CPU #ts" % i)
    label.pack()

If you need to reference them later (such as to update the text), you can save a reference in a list or dictionary: 如果以后需要引用它们(例如更新文本),则可以将引用保存在列表或字典中:

labels = []
for i in range(number_of_cpus):
    label = tk.Label(...)
    ...
    labels.append(label)
...
for label in labels:
    label.configure(...)

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

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