【问题标题】:Adding buttons next to grid在网格旁边添加按钮
【发布时间】:2021-02-14 19:37:37
【问题描述】:

我正在尝试一些看起来像这样的东西

这是一个类似于绘画的程序。我已经对网格进行了编程,当我单击一个单元格时,它将被涂成黑色,但是我不知道如何在网格旁边添加按钮。我尝试了不同的方法,但总是收到错误消息。这是我第一次使用 python 编程并使用画布,如果有人能帮我解决这个问题,我将不胜感激。

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

【问题讨论】:

    标签: python tkinter tkinter-canvas


    【解决方案1】:

    首先创建两个框架。一帧用于网格,另一帧用于按钮。使用pack 将两个框架并排放置。

    然后,您可以创建按钮并使用pack 将它们从上到下放置在右侧,并使用grid 在左侧创建方格。

    【讨论】:

      【解决方案2】:

      这是你可以如何做的一个例子:

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

      我将您的&lt;CellGrilla&gt;.grid 变量重命名为&lt;CellGrilla&gt;._grid,以便稍后我们可以使用网格而不是包。主要更改在if __name__ == "__main__" 代码中。我让你的网格占据了 4 行(因为你想要 4 个按钮)并创建了 4 个按钮。

      【讨论】: