3

I have a square of labels that make a scrabble board. (link to picture)

Code to generate:

colors = {"TWS":"red", "DWS":"pink", "TLS":"light green", "DLS":"light blue", "*":"pink"}
self.boardFrame = Frame(self.root, bd=1, relief=SUNKEN)
self.boardFrame.place(x=50, y=50, width=497, height = 497)
labels = list()
squares = list()
for i in range(16):
    for j in range(16):
        label = self.board[j][i]
        if label in self.extraList:
            entry = Frame(self.boardFrame, bd=1, relief=RAISED)
            entry.place(x=(i*31), y=(j*31), width=31, height=31)
            labels.append(func.Label(entry, text = label,
                                     height = 31, width = 31))
            if label in colors.keys():
                labels[-1].config(bg=colors[label])
            labels[-1].pack()

I would like to make it able to zoom in when the user clicks it. I have heard that you could use something like canvas. I have looked at this question but I did not understand it particularly. If I had a image for each type of square (which is possible), how could I efficiently resize every label? Something like:

def zoom(self, event):
    if math.isclose(event.x, self.x, abs_tol=self.boardWidth/2) and \
       math.isclose(event.y, self.y, abs_tol=self.boardHeight/2):
        self.height += 30
        self.width += 30
        self.x -= event.x
        self.y -= event.y
        self.label.config(height=self.height, width=self.width)
        self.label.place_configure(x = self.x, y = self.y)

I'm not sure. Any help would be welcome really. Thanks.

EDIT: When I say zoom, I mean actually zoom in. For example, zooming in 2x will only have 1/4 of the labels visible, and they will each be twice as big.

EDIT: by the way all of the code is in the tiles.py in this github repo.

Community
  • 1
  • 1
rassar
  • 5,412
  • 3
  • 25
  • 41

1 Answers1

1

As far as I know, there is no way to scale a drawing that is already drawn on tkinter's Frame or Canvas. But you can implement scaling yourself by multiplying you cells dimensions by scale factor when needed. Here is some demo code:

from tkinter import *

def drawBoard(boardFrame, scale):
   cellSize = 31 * scale

   for i in range(16):
      for j in range(16):
         entry = Frame(boardFrame, bd=1, relief=RAISED)
         entry.place(x=(i*cellSize), y=(j*cellSize), width=cellSize, height=cellSize)

def buttonCallback(event):      
    global scale
    scale = scale + 0.1
    drawBoard(boardFrame, scale)        

scale = 1.0
root = Tk()

boardFrame = Frame(root, bd=1, relief=SUNKEN)
boardFrame.place(x=50, y=50, width=497, height = 497)

drawBoard(boardFrame, scale)    
root.bind("<Button-1>", buttonCallback)

root.mainloop()
SergeyLebedev
  • 3,673
  • 15
  • 29