2016-11-29 3 views
3

나는 스크래블 보드를 만드는 사각형의 레이블을 가지고있다. (link to picture)레이블의 사각형으로 확대하기 파이썬 tkinter

코드가 생성하는 :

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() 
나는 사용자가 클릭하면 확대하는 것이 가능하고 싶다

. canvas과 같은 것을 사용할 수 있다고 들었습니다. 나는 this question을 보았습니다. 그러나 나는 그것을 특히 이해하지 못했습니다. 가능한 모든 사각형 유형에 대한 이미지가 있다면 모든 레이블의 크기를 효율적으로 조정할 수 있습니까? 다음과 같음 :

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) 

잘 모르겠습니다. 어떤 도움이라도 정말로 환영받을 것입니다. 감사.

편집 : 제가 확대라고 말하면, 실제로는 확대를 의미합니다. 예를 들어 2 배 확대하면 1/4의 레이블 만 표시되고 각각의 크기는 두 배가됩니다.

편집 : 모든 코드는 tiles.pythis github repo에 있습니다.

답변

1

내가 아는 한 이미 tkinterFrame 또는 Canvas에 그려진 그림을 크기를 조정할 방법이 없습니다. 그러나 필요한 경우 셀 크기에 배율 인수를 곱하여 크기 조정을 구현할 수 있습니다. 데모 코드는 다음과 같습니다.

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() 
+0

정말 고마워요! – rassar