如何解决更新像素大小 tkinter
我正在尝试在按下放大或缩小按钮后更新网格的像素大小。当我单击放大时,像素大小应将其大小增加 1,当我缩小时,像素大小应减少 1。我无法在不删除其中绘制的内容的情况下更新网格。这是我的代码
from tkinter import *
from tkinter import colorchooser
import math
PixelSize = 10
class Grilla:
colorCelda = "black"
colorDefault = "white"
colorBorde = "black"
bordeDefault = "black"
def __init__(self,root,master,x,y,size):
self.master = master
self.abs = x
self.ord = y
self.size = size
self.fill = False
def switch(self):
self.fill = not self.fill
def reset(self):
self.fill = False
def draw(self):
if self.master is not None:
outline = Grilla.colorBorde
fill = Grilla.colorCelda
if not self.fill:
outline = Grilla.bordeDefault
fill = Grilla.colorDefault
xmin = self.abs * self.size
xmax = xmin + self.size
ymin = self.ord * self.size
ymax = ymin + self.size
self.master.create_rectangle(xmin,ymin,xmax,ymax,fill=fill,outline=outline)
class CellGrilla(Canvas):
def __init__(self,numFil,numCol,tamGrid,*args,**kwargs):
Canvas.__init__(self,width=tamGrid * numCol,height=tamGrid * numFil,**kwargs)
self.bind("<Button-1>",self.square_clicked)
self.cellSize = tamGrid
self.pen = "draw"
self._grid = []
for row in range(numFil):
line = []
for column in range(numCol):
line.append(Grilla(master,self,column,row,tamGrid))
self._grid.append(line)
self.switched = []
self.draw()
def square_clicked(self,event):
row,column = self._coordenadas(event)
cell = self._grid[row][column]
if self.pen == "draw":
cell.switch()
if cell.fill:
self.switched.append(cell)
else:
self.switched.remove(cell)
cell.draw()
def draw(self):
for row in self._grid:
for cell in row:
cell.draw()
def _coordenadas(self,event):
row = event.y // self.cellSize
column = event.x // self.cellSize
return row,column
def switch_to_draw(self):
self.pen = "draw"
def color(self):
colorSelec = colorchooser.askcolor()[1]
if colorSelec:
Grilla.colorCelda = colorSelec
Grilla.colorBorde = colorSelec
def clear(self):
self.switched = []
for row in self._grid:
for cell in row:
cell.reset()
cell.draw()
def DDA(self):
x1 = self.switched[0].abs
x2 = self.switched[1].abs
y1 = self.switched[0].ord
y2 = self.switched[1].ord
length = abs(x2 - x1) if abs(x2 - x1) > abs(y2 - y1) else abs(y2 - y1)
dx = abs(x1 - x2) / float(length)
dy = abs(y1 - y2) / float(length)
x,y = x1,y1
for i in range(length):
if x1 < x2:
x += dx
else:
x -= dx
if y1 < y2:
y += dy
else:
y -= dy
cell = self._grid[int(y)][int(x)]
cell.switch()
cell.draw()
def bresenhamLine(self):
x1 = self.switched[0].abs
x2 = self.switched[1].abs
y1 = self.switched[0].ord
y2 = self.switched[1].ord
dx = x2 - x1
dy = y2 - y1
D = 2 * dy - dx
x,y1
for x in range(x1 + 1,x2 + 1):
if D > 0:
y += 1
D += (2 * dy - 2 * dx)
else:
D += 2 * dy
cell = self._grid[int(y)][int(x)]
cell.switch()
cell.draw()
def bresenhamCircle(self):
x1 = self.switched[0].abs
x2 = self.switched[1].abs
y1 = self.switched[0].ord
y2 = self.switched[1].ord
radius = int(math.sqrt(math.pow(x2 - x1,2) + math.pow(y2 - y1,2)))
x = 0
y = radius
switch = 3 - (2 * radius)
self.dibujarCirculo(x1,y1,y)
while x <= y:
x = x + 1
if switch < 0:
switch = switch + (4 * x) + 6
else:
switch = switch + (4 * (x - y)) + 10
y = y - 1
self.dibujarCirculo(x1,y)
def dibujarCirculo(self,xc,yc,y):
self.drawPoint(xc + x,yc + y)
self.drawPoint(xc + x,yc - y)
self.drawPoint(xc + y,yc + x)
self.drawPoint(xc + y,yc - x)
self.drawPoint(xc - x,yc + y)
self.drawPoint(xc - x,yc - y)
self.drawPoint(xc - y,yc + x)
self.drawPoint(xc - y,yc - x)
def drawPoint(self,y):
cell = self._grid[int(y)][int(x)]
cell.switch()
cell.draw()
def Ellipse(self):
x1 = self.switched[0].abs
x2 = self.switched[1].abs
y1 = self.switched[0].ord
y2 = self.switched[1].ord
radius = int(math.sqrt(math.pow(x2 - x1,2)))
x = 0
y = radius
switch = 3 - (2 * radius)
self.dibujarElipse(x1,y)
while x <= y:
x = x + 1
if switch < 0:
switch = switch + (4 * x) + 6
else:
switch = switch + (4 * (x - y)) + 10
y = y - 1
self.dibujarElipse(x1,y)
def dibujarElipse(self,yc - x)
def zoomIn(self):
global PixelSize
if PixelSize >= 10:
PixelSize = PixelSize + 1
def zoomOut(self):
global PixelSize
if PixelSize > 11:
PixelSize = PixelSize - 1
# def floodFill(self,grid,fila,columna,nuevoColor):
# color = grid[fila][columna]
if __name__ == "__main__":
app = Tk()
grid = CellGrilla(app,75,PixelSize)
grid.grid(row=1,column=1,rowspan=4,sticky="news")
colorBoton = Button(app,text="Elegir color",command=grid.color,height=1,width=30)
ddaBoton = Button(app,text="DDA",command=grid.DDA,width=30)
zoomInBoton = Button(app,text="Zoom in",command=grid.zoomIn,width=30)
zoomOutBoton = Button(app,text="Zoom out",command=grid.zoomOut,width=30)
reset_btn = Button(app,text="Borrar",command=grid.clear,width=30)
BresenhamLine = Button(app,text="Bresenham Line",command=grid.bresenhamLine,width=30)
BresenhamCircle = Button(app,text="Bresenham Circle",command=grid.bresenhamCircle,width=30)
Ellipse = Button(app,text="Elipse",width=30)
# FloodFill = Button(app,text="Rellenar",width=30)
zoomInBoton.grid(row=1,column=2,sticky="news")
zoomOutBoton.grid(row=2,sticky="news")
colorBoton.grid(row=3,sticky="news")
ddaBoton.grid(row=4,sticky="news")
reset_btn.grid(row=1,column=3,sticky="news")
BresenhamLine.grid(row=2,sticky="news")
BresenhamCircle.grid(row=3,sticky="news")
Ellipse.grid(row=4,column =3,sticky="news")
# FloodFill.grid(row=1,column=4,sticky="news")
app.mainloop()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。