在网格旁边添加按钮
Adding buttons next to grid
我正在尝试制作类似这样的东西
这是一个类似绘画的程序。我对网格进行了编程,当我点击一个单元格时,它将被涂成黑色,但我不知道如何在网格旁边添加按钮。我尝试过不同的方法,但总是收到错误消息。这是我第一次在 python 中使用 canvas 编程,如果有人能帮助我解决这个问题,我将不胜感激。
This is the code I have
from tkinter import *
class Grilla:
colorCelda = "black"
colorDefault = "white"
colorBorde = "black"
bordeDefault = "black"
def __init__(self, root, master, x, y, size):
""" Constructor of the object called by Cell(...) """
self.master = master
self.abs = x
self.ord = y
self.size = size
self.fill = False
def switch(self):
""" Switch if the cell is filled or not. """
self.fill = not self.fill
def draw(self):
# dibujar en el canvas
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, master, numFil, numCol, tamGrid, *args, **kwargs):
Canvas.__init__(self, master, width=tamGrid * numCol, height=tamGrid * numFil, *args, **kwargs)
self.cellSize = tamGrid
self.grid = []
for row in range(numFil):
line = []
for column in range(numCol):
line.append(Grilla(self, column, row, tamGrid))
self.grid.append(line)
# memorize the cells that have been modified to avoid many switching of state during mouse motion.
self.switched = []
# bind click action
self.bind("<Button-1>", self.handleMouseClick)
self.draw()
def draw(self):
for row in self.grid:
for cell in row:
cell.draw()
def _coordenadas(self, event):
row = int(event.y / self.cellSize)
column = int(event.x / self.cellSize)
return row, column
def handleMouseClick(self, event):
row, column = self._coordenadas(event)
cell = self.grid[row][column]
cell.switch()
cell.draw()
if __name__ == "__main__":
app = Tk()
# Tamaño de canvas x tamaño de pixeles
grid = CellGrilla(app, 50, 50, 10)
grid.pack()
app.mainloop()
首先创建两个框架。一帧用于网格,另一帧用于按钮。使用 pack
将两个框架并排放置。
然后,您可以创建按钮并使用 pack
将它们从上到下放置在右侧,并使用 grid
在左侧创建正方形网格。
这是您可以如何操作的示例:
from tkinter import *
class Grilla:
colorCelda = "black"
colorDefault = "white"
colorBorde = "black"
bordeDefault = "black"
def __init__(self, root, master, x, y, size):
""" Constructor of the object called by Cell(...) """
self.master = master
self.abs = x
self.ord = y
self.size = size
self.fill = False
def switch(self):
""" Switch if the cell is filled or not. """
self.fill = not self.fill
def draw(self):
# dibujar en el canvas
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, master, numFil, numCol, tamGrid, *args, **kwargs):
Canvas.__init__(self, master, width=tamGrid * numCol, height=tamGrid * numFil, *args, **kwargs)
self.cellSize = tamGrid
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)
# memorize the cells that have been modified to avoid many switching of state during mouse motion.
self.switched = []
# bind click action
self.bind("<Button-1>", self.handleMouseClick)
self.draw()
def draw(self):
for row in self._grid:
for cell in row:
cell.draw()
def _coordenadas(self, event):
row = int(event.y / self.cellSize)
column = int(event.x / self.cellSize)
return row, column
def handleMouseClick(self, event):
row, column = self._coordenadas(event)
cell = self._grid[row][column]
cell.switch()
cell.draw()
def button1_clicked(self):
pass
def button2_clicked(self):
pass
def button3_clicked(self):
pass
def button4_clicked(self):
pass
if __name__ == "__main__":
app = Tk()
# Tamaño de canvas x tamaño de pixeles
grid = CellGrilla(app, 50, 50, 10)
grid.grid(row=1, column=1, rowspan=4, sticky="news")
button1 = Button(app, text="Button 1", command=grid.button1_clicked)
button2 = Button(app, text="Button 2", command=grid.button2_clicked)
button3 = Button(app, text="Button 3", command=grid.button3_clicked)
button4 = Button(app, text="Button 4", command=grid.button4_clicked)
button1.grid(row=1, column=2, sticky="news")
button2.grid(row=2, column=2, sticky="news")
button3.grid(row=3, column=2, sticky="news")
button4.grid(row=4, column=2, sticky="news")
app.mainloop()
我将您的 <CellGrilla>.grid
变量重命名为 <CellGrilla>._grid
以便稍后我们可以使用 grid 而不是 pack。主要更改在 if __name__ == "__main__"
代码中。我让你的网格占据了 4 行(因为你想要 4 个按钮)并创建了 4 个按钮。
我正在尝试制作类似这样的东西
这是一个类似绘画的程序。我对网格进行了编程,当我点击一个单元格时,它将被涂成黑色,但我不知道如何在网格旁边添加按钮。我尝试过不同的方法,但总是收到错误消息。这是我第一次在 python 中使用 canvas 编程,如果有人能帮助我解决这个问题,我将不胜感激。
This is the code I have
from tkinter import *
class Grilla:
colorCelda = "black"
colorDefault = "white"
colorBorde = "black"
bordeDefault = "black"
def __init__(self, root, master, x, y, size):
""" Constructor of the object called by Cell(...) """
self.master = master
self.abs = x
self.ord = y
self.size = size
self.fill = False
def switch(self):
""" Switch if the cell is filled or not. """
self.fill = not self.fill
def draw(self):
# dibujar en el canvas
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, master, numFil, numCol, tamGrid, *args, **kwargs):
Canvas.__init__(self, master, width=tamGrid * numCol, height=tamGrid * numFil, *args, **kwargs)
self.cellSize = tamGrid
self.grid = []
for row in range(numFil):
line = []
for column in range(numCol):
line.append(Grilla(self, column, row, tamGrid))
self.grid.append(line)
# memorize the cells that have been modified to avoid many switching of state during mouse motion.
self.switched = []
# bind click action
self.bind("<Button-1>", self.handleMouseClick)
self.draw()
def draw(self):
for row in self.grid:
for cell in row:
cell.draw()
def _coordenadas(self, event):
row = int(event.y / self.cellSize)
column = int(event.x / self.cellSize)
return row, column
def handleMouseClick(self, event):
row, column = self._coordenadas(event)
cell = self.grid[row][column]
cell.switch()
cell.draw()
if __name__ == "__main__":
app = Tk()
# Tamaño de canvas x tamaño de pixeles
grid = CellGrilla(app, 50, 50, 10)
grid.pack()
app.mainloop()
首先创建两个框架。一帧用于网格,另一帧用于按钮。使用 pack
将两个框架并排放置。
然后,您可以创建按钮并使用 pack
将它们从上到下放置在右侧,并使用 grid
在左侧创建正方形网格。
这是您可以如何操作的示例:
from tkinter import *
class Grilla:
colorCelda = "black"
colorDefault = "white"
colorBorde = "black"
bordeDefault = "black"
def __init__(self, root, master, x, y, size):
""" Constructor of the object called by Cell(...) """
self.master = master
self.abs = x
self.ord = y
self.size = size
self.fill = False
def switch(self):
""" Switch if the cell is filled or not. """
self.fill = not self.fill
def draw(self):
# dibujar en el canvas
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, master, numFil, numCol, tamGrid, *args, **kwargs):
Canvas.__init__(self, master, width=tamGrid * numCol, height=tamGrid * numFil, *args, **kwargs)
self.cellSize = tamGrid
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)
# memorize the cells that have been modified to avoid many switching of state during mouse motion.
self.switched = []
# bind click action
self.bind("<Button-1>", self.handleMouseClick)
self.draw()
def draw(self):
for row in self._grid:
for cell in row:
cell.draw()
def _coordenadas(self, event):
row = int(event.y / self.cellSize)
column = int(event.x / self.cellSize)
return row, column
def handleMouseClick(self, event):
row, column = self._coordenadas(event)
cell = self._grid[row][column]
cell.switch()
cell.draw()
def button1_clicked(self):
pass
def button2_clicked(self):
pass
def button3_clicked(self):
pass
def button4_clicked(self):
pass
if __name__ == "__main__":
app = Tk()
# Tamaño de canvas x tamaño de pixeles
grid = CellGrilla(app, 50, 50, 10)
grid.grid(row=1, column=1, rowspan=4, sticky="news")
button1 = Button(app, text="Button 1", command=grid.button1_clicked)
button2 = Button(app, text="Button 2", command=grid.button2_clicked)
button3 = Button(app, text="Button 3", command=grid.button3_clicked)
button4 = Button(app, text="Button 4", command=grid.button4_clicked)
button1.grid(row=1, column=2, sticky="news")
button2.grid(row=2, column=2, sticky="news")
button3.grid(row=3, column=2, sticky="news")
button4.grid(row=4, column=2, sticky="news")
app.mainloop()
我将您的 <CellGrilla>.grid
变量重命名为 <CellGrilla>._grid
以便稍后我们可以使用 grid 而不是 pack。主要更改在 if __name__ == "__main__"
代码中。我让你的网格占据了 4 行(因为你想要 4 个按钮)并创建了 4 个按钮。