[英]Creating multiple differently colored shapes using tkinter and classes
I'm trying to create a program that will draw 16 different squares, in four different colors.我正在尝试创建一个程序,该程序将在四个不同的 colors 中绘制 16 个不同的正方形。 So for example, the code for a red piece looks like this:
例如,红色部分的代码如下所示:
redSquare = Label(window,bg="red",width=2,height=2)
redSquare.place(x=5,y=5)
Now, instead of copy and pasting this multiple time in my code, would there be a way to create a class, where the changeable attribute is the color and position?现在,不是在我的代码中多次复制和粘贴,有没有办法创建一个 class,其中可变属性是颜色和 position? And if so, how would that code look?
如果是这样,该代码的外观如何?
I wrote this code now using the best practices that I have learned so far.我现在使用迄今为止学到的最佳实践编写了这段代码。 The class inherits from a
Frame
which means, you have a frame with all these colors gridded inside of it. class 继承自
Frame
,这意味着,您有一个框架,所有这些 colors 都在其中网格化。 This gives the colors in a table format.这给出了表格格式的 colors。 I would not recommend using pixels as it is not dynamic with different screen resolutions.
我不建议使用像素,因为它在不同的屏幕分辨率下不是动态的。 You will have to create a list of colors for all the rows and columns, if not, colors will be repeated.
您必须为所有行和列创建 colors 列表,如果没有,则将重复 colors。 Take a look:
看一看:
from tkinter import *
class SquareGenerator(Frame): # Inherit from tkinter Frame
def __init__(self,parent:Tk,color_lst:list,rows:int,columns:int,side:int,padx:int=0,pady:int=0,*args,**kwargs):
Frame.__init__(self,parent,*args,**kwargs)
img = PhotoImage(height=1,width=1) # To use pixels with label
if len(color_lst) != rows*columns: # If not enough colors are given
print('WARNING: Did not recieve a valid color list, using custom list')
if len(color_lst) < rows*columns:
if (rows*columns)-len(color_lst) == 1:
color_lst.append(color_lst[0])
else:
color_lst = color_lst*((rows*columns)-len(color_lst)) # Repeat the list enough times
else:
color_lst = color_lst[:rows*columns]
# Table loop
for i in range(rows):
for j in range(columns):
each_color = color_lst[columns*i+j] # Index each item in list
l = Label(self,bg=each_color,image=img,width=side,height=side) # Create label
l.grid(row=i,column=j,padx=padx,pady=pady)
if __name__ == '__main__':
root = Tk()
colors = ['red','green','blue','orange']
gen = SquareGenerator(root,colors,rows=5,columns=1,side=100) # Replicate microsoft logo ;)
gen.pack()
root.mainloop()
This will create a frame with each colors in the given list kept in 2 rows and 2 columns.这将创建一个框架,将给定列表中的每个 colors 保存在 2 行和 2 列中。 So total 4 colors are needed to be defined.
所以总共需要定义4个colors。 You can play around and try passing in less than or more than 4 colors and see what happens with current code too.
您可以四处玩耍并尝试传入少于或多于 4 个 colors 并查看当前代码会发生什么。
However I used classes, just because you had asked for it, a non OOP approach would be:但是我使用了类,只是因为您要求它,非 OOP 方法将是:
from tkinter import *
root = Tk()
frame = Frame(root)
frame.pack()
ROWS = 5
COLUMNS = 1
PADX = 0
PADY = 0
SIDE_LENGTH = 100
colors = ['red','green','blue','orange']
if len(colors) != ROWS*COLUMNS: # If not enough colors are given
print('WARNING: Did not recieve a valid color list, using custom list')
if len(colors) > ROWS*COLUMNS:
colors = colors[:ROWS*COLUMNS]
else:
if (ROWS*COLUMNS)-len(colors) == 1:
colors.append(colors[0])
else:
colors = colors*((ROWS*COLUMNS)-len(colors)) # Repeat the list enough times
img = PhotoImage(height=1,width=1) # To use pixels with label
for i in range(ROWS):
for j in range(COLUMNS):
each_color = colors[COLUMNS*i+j] # Index each item in list
l = Label(frame,bg=each_color,image=img,width=SIDE_LENGTH,height=SIDE_LENGTH) # Create label
l.grid(row=i,column=j,padx=PADX,pady=PADY)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.