天天看点

python如何创建按钮_Python如何创建一个按钮矩阵并将其放入我的一个框架中

from tkinter import *

import tkinter .messagebox

root = Tk()

root.geometry("1350x750+0+0")

root.title("Tres en raya")

root.configure(background = "#FFFFD1")

Tops = Frame(root, bg = "white", pady = 2, width = 1350, height = 100, relief=RIDGE)

Tops.grid(row = 0, column = 0)

lblTitle = Label(Tops, font = ("Tohama", 50, "bold"), text = "*** TRES EN RAYA ***", bd=21, bg="#b28dff", fg="cornsilk", justify="center")

lblTitle.grid(row = 0, column = 1)

MainFrame = Frame(root, bg = "#C5A3FF", bd = 10, width = 1350, height = 600, relief = RIDGE) #ROSA

MainFrame.grid(row = 1, column = 0)

LeftFrame = Frame (MainFrame, bd=10, width = 560, height = 500, pady = 2, padx = 10, bg = "#85E3FF", relief= RIDGE)

LeftFrame.pack(side = LEFT)

RightFrame = Frame (MainFrame, bd=10, width = 560, height = 500, pady = 10, padx = 2, bg = "#85E3FF", relief= RIDGE)

RightFrame.pack(side = RIGHT)

RightFrame1 = Frame(RightFrame, bd=10, width = 560, height = 200, pady = 10, padx = 2, bg = "#85E3FF", relief= RIDGE)

RightFrame1.grid(row = 0, column = 0)

RightFrame2 = Frame (RightFrame, bd=10, width = 560, height = 200, pady = 10, padx = 2, bg = "#85E3FF", relief= RIDGE)

RightFrame2.grid(row = 1, column = 0)

playerX = IntVar()

player0 = IntVar()

playerX.set(0)

player0.set(0)

buttons = StringVar()

click = True

def checker(buttons):

global click

if buttons["text"] == "" and click == False:

buttons["text"] = "O"

click = True

scorekeeper()

elif buttons["text"] == "" and click == True:

buttons["text"] = "X"

click = False

scorekeeper()

def scorekeeper():

global tie

global button1, button2, button3, button4, button5, button6, button7, button8, button9

lblscore = Label(RightFrame1, font = ("tahoma", 30, "bold"),text = "*** TABLA DE PUNTUACIONES", padx = 2, pady = 2)

lblscore.grid(row = 0, column =1)

lblplayerX = Label(RightFrame2, font = ("tahoma", 25, "bold"), bd = 2, fg="black", textvariable=playerX, width = 14)

lblplayer0 = Label(RightFrame2, font = ("tahoma", 25, "bold"), bd = 2, fg="black", textvariable=player0, width = 14)

lblnombrex = Label(RightFrame2, font = ("tahoma", 25, "bold"),text = "Jugador Uno", bd = 2, fg="black", width = 14)

lblnombre0 = Label(RightFrame2, font = ("tahoma", 25, "bold"),text = "Jugador Dos", bd = 2, fg="black", width = 14)

lblplayerX.grid(row = 3, column = 2, padx = 6, pady = 5)

lblnombrex.grid(row = 3, column = 1, padx = 6, pady = 5)

lblplayer0.grid(row = 4, column = 2, padx = 6, pady = 5)

lblnombre0.grid(row = 4, column = 1, padx = 6, pady = 5)

button1 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button1))

button1.grid(row = 1, column =1)

button2 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button2))

button2.grid(row = 1, column = 2)

button3 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button3))

button3.grid(row = 1, column =3)

button4 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button4))

button4.grid(row = 2, column =1)

button5 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button5))

button5.grid(row = 2, column =2)

button6 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button6))

button6.grid(row = 2, column =3)

button7 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button7))

button7.grid(row = 3, column =1)

button8 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button8))

button8.grid(row = 3, column =2)

button9 = Button(LeftFrame, text = "", font=("tahoma", 30, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(button9))

button9.grid(row = 3, column =3)

root.mainloop()

我是python新手,我看到了一个制作这个框架的教程,我想使用矩阵而不是单个按钮,因为我需要实现min-max算法。

我试过了

botones = [[],[],[]]

for i in range(3):

for j in range(3):

botones[i].append(None)

botones[i][j] = Button(LeftFrame, text = "", font=("tahoma", 25, "bold"), height = 3, width = 8, bg="gainsboro",command=lambda:checker(botones[i][j])).grid(row = i+1, column = j+1)