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?
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()
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With