我正在設定一個簡單的 GUI 來顯示網路埠的狀態。如果我為每個標簽使用單獨的命令,這很好:
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()
在職的
但是,當我創建一個類然后設定一個串列來做同樣的事情時,我只看到一個標簽顯示:
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 還很陌生,所以我很有信心這是我在這里做錯的事情。有什么建議?
uj5u.com熱心網友回復:
您已使用class variables而不是instance variables. 一個類只有一組,因此您在每次迭代中都class variables覆寫了。class variables
下面是一個使用示例instance variables:
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)
uj5u.com熱心網友回復:
我已經設法對其進行了調整,以便您在基于類的結構下保持 GUI 的原始比例:
from tkinter import *
class NetworkPort:
def __init__(self, master):
self.master = master
master.geometry('400x225')
master.configure(background='white')
self.num_font_size = 10
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)
if __name__ == "__main__":
root = Tk()
NetworkPort(root)
root.mainloop()
希望這會有所幫助,請隨時查詢您感興趣的任何內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/416146.html
標籤:
