【问题标题】:Is there a way to assign values to individual buttons I've created using root.Button.grid in tkinter?有没有办法为我在 tkinter 中使用 root.Button.grid 创建的单个按钮分配值?
【发布时间】:2019-04-08 23:16:25
【问题描述】:

我正在用 python/tkinter 制作井字游戏。

所以我使用 xxx.Button.grid 绘制了一个 3x3 网格,现在我想为每个按钮分配属性,以便我可以输入确定“X”或“O”的文本

我想要做的是将 1 或 (-1) 分配给具有 0 值的空方格,从而确定 X 和 O。我希望以这种方式接近它可以帮助我找到获胜条件。

from tkinter import Tk, Canvas, Frame, Grid, Button, N, S, E, W


root = Tk()
canvas = Canvas()
frame = Frame(root)

root.title("DJB")

root.minsize(300, 300)
root.resizable(False, False)

root.configure(bg='black')
# --------------------------------------------

Grid.rowconfigure(root, 3, weight=1)
Grid.columnconfigure(root, 3, weight=1)

frame=Frame(root)
frame.grid(row=3, column=3, sticky=N+S+E+W)


for row_index in range(3):
    Grid.rowconfigure(frame, row_index, weight=1)
    for col_index in range(3):
        Grid.columnconfigure(frame, col_index, weight=1)
        btn = Button(frame, bg="white") #create a button inside frame
        btn.grid(row=row_index, column=col_index, sticky=N+S+E+W)

如果我可以只为每个按钮输入代码,它就会推动我的项目。最终,我制作了一个按钮网格,按下时会显示文本并在 +1 和 -1 之间切换整数。

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    您可以通过widget["text"]查看您的按钮文字。将此作为命令传递以动态检索值:

    from tkinter import Tk, Canvas, Frame, Grid, Button, N, S, E, W
    
    root = Tk()
    canvas = Canvas()
    frame = Frame(root)
    
    root.title("DJB")
    
    root.minsize(300, 300)
    root.resizable(False, False)
    
    root.configure(bg='black')
    # --------------------------------------------
    
    Grid.rowconfigure(root, 3, weight=1)
    Grid.columnconfigure(root, 3, weight=1)
    
    frame.grid(row=3, column=3, sticky=N+S+E+W)
    
    def get_value(widget):
        if widget["text"] == "" or widget["text"] == "X":
            widget["text"] = "O"
            #value = 1
        else:
            widget["text"] = "X"
            #value = -1
    
    for row_index in range(3):
        Grid.rowconfigure(frame, row_index, weight=1)
        for col_index in range(3):
            Grid.columnconfigure(frame, col_index, weight=1)
            btn = Button(frame, bg="white",font="Arial 40 bold",width=3)
            btn.config(command=lambda x=btn: get_value(x))
            btn.grid(row=row_index, column=col_index, sticky=N+S+E+W)
    
    root.mainloop()
    

    【讨论】:

      【解决方案2】:

      我更改了你的代码,如果这对你有帮助,请告诉我。

      我添加了一个函数,该函数将返回按下按钮的 id。

      功能:

      # This Function will return Button id 
      def button_id(id):
          print("Button {} is pressed".format(id))
          return id
      

      完整代码:

      from tkinter import Tk, Canvas, Frame, Grid, Button, N, S, E, W, mainloop
      from functools import partial
      
      root = Tk()
      canvas = Canvas()
      frame = Frame(root)
      
      root.title("DJB")
      
      root.minsize(300, 300)
      root.resizable(False, False)
      
      root.configure(bg='black')
      # --------------------------------------------
      
      Grid.rowconfigure(root, 3, weight=1)
      Grid.columnconfigure(root, 3, weight=1)
      
      frame=Frame(root)
      frame.grid(row=3, column=3, sticky=N+S+E+W)
      
      # This Function will return Button id 
      def button_id(id):
          print("Button {} is pressed".format(id))
          return id
      
      id = 1
      
      for row_index in range(3):
          Grid.rowconfigure(frame, row_index, weight=1)
          for col_index in range(3):
              Grid.columnconfigure(frame, col_index, weight=1)
              btn = Button(frame, bg="white", command = partial(button_id, id=id)) #create a button inside frame
              btn.grid(row=row_index, column=col_index, sticky=N+S+E+W)
              id += 1
      
      mainloop()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-29
        • 2020-08-17
        • 1970-01-01
        • 2014-10-19
        相关资源
        最近更新 更多