首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有正确显示的标签列表

没有正确显示的标签列表
EN

Stack Overflow用户
提问于 2022-01-19 23:16:44
回答 2查看 59关注 0票数 2

我正在设置一个简单的GUI来显示网络端口的状态。如果我对每个标签使用单独的命令,这会很好:

代码语言:javascript
复制
from tkinter import *

root = Tk()
root.geometry('400x225')
root.configure(background='white')
pFontsize = 10

p1 = Label(root, text = '1', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 1/14, rely = 2/10)
p2 = Label(root, text = '2', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 2/14, rely = 2/10)
p3 = Label(root, text = '3', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 3/14, rely = 2/10)
p4 = Label(root, text = '4', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 4/14, rely = 2/10)
p5 = Label(root, text = '5', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 5/14, rely = 2/10)
p6 = Label(root, text = '6', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 6/14, rely = 2/10)
p7 = Label(root, text = '7', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 7/14, rely = 2/10)
p8 = Label(root, text = '8', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 8/14, rely = 2/10)
p9 = Label(root, text = '9', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 9/14, rely = 2/10)
p10 = Label(root, text = '10', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 10/14, rely = 2/10)
p11 = Label(root, text = '11', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 11/14, rely = 2/10)
p12 = Label(root, text = '12', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 12/14, rely = 2/10)
p13 = Label(root, text = '13', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 1/14, rely = 5/10)
p14 = Label(root, text = '14', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 2/14, rely = 5/10)
p15 = Label(root, text = '15', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 3/14, rely = 5/10)
p16 = Label(root, text = '16', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 4/14, rely = 5/10)
p17 = Label(root, text = '17', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 5/14, rely = 5/10)
p18 = Label(root, text = '18', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 6/14, rely = 5/10)
p19 = Label(root, text = '19', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 7/14, rely = 5/10)
p20 = Label(root, text = '20', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 8/14, rely = 5/10)
p21 = Label(root, text = '21', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 9/14, rely = 5/10)
p22 = Label(root, text = '22', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 10/14, rely = 5/10)
p23 = Label(root, text = '23', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 11/14, rely = 5/10)
p24 = Label(root, text = '24', background = 'white',font=('Calibri', pFontsize),anchor='center').place(relwidth = 1/14, relheight = 1/10, relx = 12/14, rely = 5/10)

i1 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 1/14, rely = 3/10)
i2 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 2/14, rely = 3/10)
i3 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 3/14, rely = 3/10)
i4 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 4/14, rely = 3/10)
i5 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 5/14, rely = 3/10)
i6 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 6/14, rely = 3/10)
i7 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 7/14, rely = 3/10)
i8 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 8/14, rely = 3/10)
i9 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 9/14, rely = 3/10)
i10 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 10/14, rely = 3/10)
i11 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 11/14, rely = 3/10)
i12 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 12/14, rely = 3/10)
i13 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 1/14, rely = 6/10)
i14 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 2/14, rely = 6/10)
i15 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 3/14, rely = 6/10)
i16 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 4/14, rely = 6/10)
i17 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 5/14, rely = 6/10)
i18 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 6/14, rely = 6/10)
i19 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 7/14, rely = 6/10)
i20 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 8/14, rely = 6/10)
i21 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 9/14, rely = 6/10)
i22 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 10/14, rely = 6/10)
i23 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 11/14, rely = 6/10)
i24 = Label(root, background = 'black').place(relwidth = 1/14, relheight = 1/10, relx = 12/14, rely = 6/10)

root.mainloop()

工作中

然而,当我创建一个类然后设置一个列表来做同样的事情时,我只看到一个标签显示:

代码语言:javascript
复制
from tkinter import *

root=Tk()
root.geometry('400x225')
#root.attributes('-fullscreen', True)
root.configure(background='white')

class networkPort:
    numFontsize = 10
    num = Label(root, background = 'white',font=('Calibri', numFontsize),anchor='center')
    indicator = Label(root, background = 'black')
 
ports = []
for i in range(24):
    ports.append(networkPort)
    ports[i].num.configure(text = str(i+1))
    if i > 11:
        y = 4
    else:
        y = 2
    ports[i].num.place(relwidth = 1/14, relheight = 1/10, relx = (i%12 + 1)/14, rely = y/10)
    ports[i].indicator.place(relwidth = 1/14, relheight = 1/10, relx = (i%12 + 1)/14, rely = y+1/10)

root.mainloop()

不工作

我对Python相当陌生,所以我很有信心我在这里做错了什么。有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-20 01:38:30

您使用的是class variables而不是instance variables。类只有一组class variables,所以在每次迭代中都要覆盖class variables

下面是使用instance variables的示例

代码语言:javascript
复制
class NetworkPort:
    def __init__(self, master, num):
        numFontsize = 10
        self.num = Label(master, text=num, background='white', font=('Calibri', numFontsize), anchor='center')
        self.indicator = Label(master, background='black')

ports = []
for i in range(24):
    ports.append(NetworkPort(root, i+1))
    y = 4 if i > 11 else 2
    ports[i].num.place(relwidth=1/14, relheight=1/10, relx=(i%12+1)/14, rely=y/10)
    ports[i].indicator.place(relwidth=1/14, relheight=1/10, relx=(i%12+1)/14, rely=(y+1)/10)
票数 2
EN

Stack Overflow用户

发布于 2022-01-20 00:50:09

我已经对其进行了调整,因此您可以在基于类的结构下维护GUI的原始比例:

代码语言:javascript
复制
from tkinter import *


class NetworkPort:
    def __init__(self, master):
        self.master = master
        master.geometry('400x225')
        master.configure(background='white')
        self.num_font_size = 10
        
        # List to be populated with indicator widgets
        self.widgets = []

        x = -1
        y = 0

        for i in range(24):

            # On 12 iteration move to new line (y), reset to first column (x)
            if i == 12:
                x = 0
                y = 3

            # Move to new column with each iteration
            else:
                x += 1

            # Generate network port number
            num = Label(master, background='white', text=i + 1, font=('Calibri', self.num_font_size), anchor='center')
            num.place(relwidth=1 / 14, relheight=1 / 10, relx=(x + 1) / 14, rely=(y + 2) / 10)

            # Generate network indicator label
            indicator = Label(master, background='black')
            indicator.place(relwidth=1 / 14, relheight=1 / 10, relx=(x + 1) / 14, rely=(y + 3) / 10)

            # Add indicator label objects to list
            self.widgets.append(indicator)

        # Example of adjusting one specific indicators colour by accessing the widget list
        self.widgets[3].config(bg='red')


if __name__ == "__main__":
    root = Tk()
    NetworkPort(root)
    root.mainloop()

希望这能帮上忙,你可以随意地询问你好奇的事情。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70778972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档