放大标签的正方形 python tkinter
Zoom into square of labels python tkinter
我有一个方形的标签,可以用来制作拼字板。 (link to picture)
要生成的代码:
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 的标签可见,并且每个标签都是原来的两倍大。
编辑:顺便说一句,所有代码都在 this github repo 中的 tiles.py
中。
据我所知,无法缩放已在 tkinter
的 Frame
或 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()
我有一个方形的标签,可以用来制作拼字板。 (link to picture)
要生成的代码:
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 的标签可见,并且每个标签都是原来的两倍大。
编辑:顺便说一句,所有代码都在 this github repo 中的 tiles.py
中。
据我所知,无法缩放已在 tkinter
的 Frame
或 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()