简体   繁体   English

如何根据使用 tkinter 和 python 的 quixo 游戏在按钮网格上的每个按钮上添加文本

[英]How Do I add text on every button on a grid of buttons depending on a game of quixo using tkinter and python

I use python to create a GUI for this game of quixo.我使用 python 为这个 quixo 游戏创建 GUI。 but I dont need to play on the GUI.但我不需要在 GUI 上玩。 just to show the players movements on it只是为了向玩家展示它的动作

from tkinter import *

    root = Tk()
    root.title("QUIXO")

    for i in range(1,6):
        for j in range(1,6):

            myButton = Button(root, text="0", padx = 20, pady = 20)
            myButton.grid(row=i, column=j)


    root.mainloop()

I have this code which make the 5x5 grid of buttons.我有这段代码可以制作 5x5 的按钮网格。 I use buttons because I dont know what else to use.我使用按钮是因为我不知道还能使用什么。 but on this grid I need to add X and 0 depending on where they are on the quixo board.但在这个网格上,我需要根据它们在 quixo 板上的位置添加 X 和 0。

essentially I have a matrix本质上我有一个矩阵

as an example举个例子

[[-,-,-,-,X]
[-,-,-,-,X]
[-,-,-,0,-]
[0,-,-,0,X]
[0,-,0,0,-]] 

how can I place this on that grid of buttons?我怎样才能把它放在那个按钮网格上? the buttons don't need to be clicked.不需要点击按钮。 the are just there to show info只是在那里显示信息

This is how I would do it, might not be the most elegant solution:这就是我的做法,可能不是最优雅的解决方案:

from tkinter import *

root = Tk()
root.title("QUIXO")

buttonmatrix = [["" for i in range(5)] for i in range(5)] #Creates a 5x5 matrix with 0s as placeholders

#Then iterate through the matrix assigning buttons to each index and gridding them in the tkinter GUI

for i in range(5):
    for j in range(5):
        buttonmatrix[i][j]  = Button(root, text="0", state = "disabled", padx = 20, pady = 20)
        buttonmatrix[i][j].grid(row = i, column = j)

def updatebuttongrid(matrix):  #A function that updates the text of the buttons in the grid to a matrix passed to it.
    for i in range(5):
        for j in range(5):
            buttonmatrix[i][j].configure(text = str(matrix[i][j]))


example = [["-","-","-","-","X"],
["-","-","-","-","X"],
["-","-","-",0,"-"],
[0,"-","-","0","X"],
[0,"-",0,0,"-"]]

updatebuttongrid(example) #call the function whenever you want to update the GUI

root.mainloop()

Let me know if this helps:)让我知道这是否有帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用网格方法在 Tkinter 中对齐单选按钮? - How do I align Radio Buttons in Tkinter Using Grid Method? 如何使用Python Tkinter修复与按钮文本无关的大小? - How with Python Tkinter do I fix the size irrelevant of text of buttons? 如何使用.grid方法将滚动条添加到tkinter文本小部件 - How do I add a Scrollbar to a tkinter Text Widget using .grid method 如何使用tkinter放置按钮或文本框或标签? - How do I position buttons or text boxes or labels using tkinter? 您如何更新用于国际象棋游戏的 tkinter 按钮的文本? - How do you update the text of tkinter buttons for use in a chess game? 将Python与Tkinter结合使用,如何根据选项菜单中选择的选项,使按钮按下做不同的事情? - Using Python with Tkinter, how can I make a button press do a different thing depending on which option is selected in the option menu? 如何使用 tkinter 中的 2D 列表访问按钮网格中的按钮? - How to access a button in a grid of buttons using a 2D list in tkinter? Python 使用 Tkinter 的 GUI 扫雷游戏 如何正确显示在按钮上? - Python Minesweeper game with GUI using Tkinter How to display on buttons correctly? 在Python 2.6中使用Tkinter,如何使按钮小部件在文本框中打印出选中的Checkbuttons? - Using Tkinter in Python 2.6 how do I make a button widget print out selected Checkbuttons in a text box? 如何使用 tkinter 中的按钮调用单独的 python 程序? - How do I call a separate python program using a button in tkinter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM