问题描述
限时送ChatGPT账号..我正在尝试创建一个程序,该程序将以四种不同的颜色绘制 16 个不同的正方形.例如,红色部分的代码如下所示:
I'm trying to create a program that will draw 16 different squares, in four different colors. 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? And if so, how would that code look?
推荐答案
我现在使用迄今为止学到的最佳实践编写了这段代码.该类从 Frame
继承,这意味着,您有一个框架,所有这些颜色都在其内部网格化.这给出了表格格式的颜色.我不建议使用像素,因为它在不同屏幕分辨率下不是动态的.您必须为所有行和列创建一个颜色列表,否则,颜色将重复.看一看:
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. This gives the colors in a table format. 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. 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()
这将创建一个框架,其中给定列表中的每种颜色保持在 2 行和 2 列中.所以总共需要定义4种颜色.您可以尝试并尝试传入少于或多于 4 种颜色,看看当前代码会发生什么.
This will create a frame with each colors in the given list kept in 2 rows and 2 columns. So total 4 colors are needed to be defined. You can play around and try passing in less than or more than 4 colors and see what happens with current code too.
但是我使用了类,仅仅因为您要求它,非 OOP 方法是:
However I used classes, just because you had asked for it, a non OOP approach would be:
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()
这篇关于使用 tkinter 和类创建多个不同颜色的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论