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

时间:2016-04-30 00:54:47

标签: python python-2.7 python-3.x tkinter tkinter-canvas

尝试创建一个创建64个按钮的方法。此方法将按钮附加到二维列表,并使用.grid()将它们放在tkinter画布上。不完全确定问题是什么。当运行代码时,它几乎没有任何错误,没有代码0甚至没有tkinter窗口出现。该方法如下所示。我还发布了大师班,任何帮助。

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)

我还会添加主类,以防出现问题。

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

2 个答案:

答案 0 :(得分:0)

它对我很有用,它确实在根上创建了一个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 matrix 如果您仍然遇到问题,那可能是因为代码中的其他部分,所以请发布更广泛的代码块以便我们找到问题。

答案 1 :(得分:0)

经过进一步调试后,显然使用带有.grid()的.pack()被证明非常混乱。通过将.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")