简体   繁体   English

创建用于存储Tkinter按钮的2D列表

[英]Creating a 2D List To Store Tkinter Buttons

Trying to create a method that creates 64 buttons. 尝试创建一个创建64个按钮的方法。 This method will append the buttons to a two dimensional list and put them on the tkinter canvas using .grid(). 此方法将按钮附加到二维列表,并使用.grid()将它们放在tkinter画布上。 Not exactly sure what the problem is. 不完全确定问题是什么。 When running the code it does literally nothing, no error, no code 0 not even the tkinter window appears. 当运行代码时,它几乎没有任何错误,没有代码0甚至没有tkinter窗口出现。 The method is below with the rest of its class. 该方法如下所示。 I also posted the master class, anything helps. 我还发布了大师班,任何帮助。

import Tkinter as Tk
import ttk

# Fonts used throughout the class

LARGE_FONT = ("Verdana", 14)
SMALL_FONT = ("Verdana", 10)

# Game Board

board = [['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'],
         ['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'],
         [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
         [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
         [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
         [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
         ['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'],
         ['R', 'N', 'B', 'K', 'Q', 'B', 'N', 'R']
         ]


class Board(Tk.Frame):

    def __init__(self, parent, controller):

        Tk.Frame.__init__(self, parent)

        # init variables
        self.turn = True
        self.buttons = []
        self.board = board

        # Header
        label = Tk.Label(self, text="Chess", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        # Game
        # Sets game board
        self.set_buttons()

        # Sets game board
        # self.set_buttons2()

        back_button = ttk.Button(self, text="Main Menu", command=self.main_menu(controller))
        back_button.pack(side=Tk.BOTTOM, fill=Tk.BOTH)

    # Stores buttons in list for later use
    # Places buttons on canvas.
    def set_buttons(self):
        for i in range(8):
            self.buttons.append([])
            for j in range(8):
                self.buttons[i].append(ttk.Button(self, text=self.board[i][j]))
                self.buttons[i][j].configure(command=self.piece_control)
                self.buttons[i][j].grid(row=i, column=j, sticky="nsew")

    def piece_control(self):
        pass

    @staticmethod
    def main_menu(controller):
        from MainMenu import MainMenu
        return lambda: controller.show_frame(MainMenu)

I will also add the master class just in case that is where the problem is. 我还将添加主类,以防万一问题出在哪里。

from MainMenu import MainMenu
from Board import Board
from Rules import Rules
from Options import Options
import Tkinter as Tk


class Main(Tk.Tk):

    def __init__(self, *args, **kwargs):

    Tk.Tk.__init__(self, *args, **kwargs)

        Tk.Tk.title(self, "Chess")

        container = Tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (MainMenu, Board, Options, Rules):

            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(MainMenu)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

# Creates Object of main and loops it.

Application = Main()
Application.geometry("650x650")
Application.mainloop()

it works great for me, it does create an 8x8 matrix on the root. 它对我很有用,它确实在根上创建了一个8x8矩阵。

import ttk
from Tkinter import *
root = Tk()
buttons = []

def set_buttons():
    for i in range(8):
        buttons.append([])
        for j in range(8):[![enter image description here][1]][1]
            buttons[i].append(ttk.Button(text='3'))
            buttons[i][j].configure(command=com)
            buttons[i][j].grid(row=i, column=j, sticky="nsew")

def com():
    print 3

set_buttons()
root.mainloop()

8x8矩阵 if you're still having problems then its probably because of other parts in your code, so please post a more extensive code block for us to find the problem. 如果您仍然遇到问题,那可能是因为代码中的其他部分,所以请发布更广泛的代码块以便我们找到问题。

After further debugging apparently using .pack() with .grid() was proving to be quite chaotic. 经过进一步的调试后,显然使用带有.grid()的.pack()被证明是非常混乱的。 Made a few simple changes to the init function by changing the .pack() methods to .grid() and it works like a charm. 通过将.pack()方法更改为.grid()对init函数进行了一些简单的更改,它就像一个魅力。

def __init__(self, parent, controller):

    Tk.Frame.__init__(self, parent)

    # init variables
    self.turn = True
    self.buttons = []
    self.board = board

    # Header
    label = Tk.Label(self, text="Chess", font=LARGE_FONT)
    label.grid(row=0, column=3, columnspan=2, sticky="nsew")

    # Game
    # Sets game board
    self.set_buttons()

    back_button = ttk.Button(self, text="Main Menu", command=self.main_menu(controller))
    back_button.grid(row=8, column=3, columnspan=2, sticky="nsew")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM