繁体   English   中英

Tkinter,有两个独立的板子可以参考

[英]Tkinter, have two separate boards that can be referenced

问题可能出在评论部分。

我必须在屏幕上有两个选项卡。 这两个棋盘必须相互交互:如果我单击一个棋盘的白色或黑色按钮,则在另一个棋盘上相应的红色按钮必须分别变为白色或黑色; 相反,如果您单击两个红色按钮,则必须具有有关“信息内容”的信息。

Se invece introduco il secondo tabellone, accadono i disastri。 La prima scheda non subisce modifiche, le modifiche interessano solo la seconda scheda。

我怎样才能有两个单独的板,但它们可以被引用?

import tkinter as tk
import numpy as np
import random

# random generator of visible part
visible = np.zeros((6,6))
count1 = 0
count2 = 0

for i in range(6):
    for j in range(6):
        if count1 == 18:
            visible[i][j] = 1
            count2 = count2+1
        elif count2 == 18:
            visible[i][j] = 0
            count1 = count1+1
        elif np.random.uniform(0,1) <= 0.5 and count1 < 19:
            visible[i][j] = 0
            count1 = count1+1
        else:
            visible[i][j] = 1
            count2 = count2+1

print(visible)

# random generator of invisible part
img = np.zeros((6,6))
count1 = 0
count2 = 0

for i in range(6):
    for j in range(6):
        if count1 == 28:
            img[i][j] = 1
            count2 = count2+1
        elif count2 == 8:
            img[i][j] = 0
            count1 = count1+1
        elif np.random.uniform(0,1) <= 0.75 and count1 < 31:
            img[i][j] = 0
            count1 = count1+1
        else:
            img[i][j] = 1
            count2 = count2+1

print(img)

click = 0
start = 0
finish = 0

class Layout(tk.Tk):

    def __init__(self, n=6):
        super().__init__()
        self.n = n
        self.leftframe = tk.Frame(self)
        self.leftframe.grid(row=0, column=0, rowspan=10, padx=100)
        self.middleframe = tk.Frame(self)
        self.middleframe.grid(row=0, column=6, rowspan=6)
        self.canvas = tk.Canvas(self, width=1000, height=500, )
        self.canvas.grid(row=0, column=1, columnspan=6, rowspan=6)
        self.board = [[None for row in range(n)] for col in range(n)]

    def drawboard(self):
        for col in range(self.n):
            color = "#ff0000"
            for row in range(self.n):
                x1 = col * 60
                y1 = (5-row) * 60
                x2 = x1 + 60
                y2 = y1 + 60
                if(visible[5-row][col] == 0):
                    self.board[row][col] = self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, tags=f"tile{col+1}{row+1}")
                    self.canvas.tag_bind(f"tile{col+1}{row+1}","<Button-1>", lambda e, i=col, j=row: self.get_location(e,i,j))
                else:
                    if(img[5-row][col] == 0):
                        self.board[row][col] = self.canvas.create_rectangle(x1+5, y1+5, x2-5, y2-5, fill="white", tags=f"tile{col+1}{row+1}")
                        self.canvas.tag_bind(f"tile{col+1}{row+1}","<Button-1>", lambda e, i=col, j=row: self.get_info(e,i,j))
                    else:
                        self.board[row][col] = self.canvas.create_rectangle(x1+5, y1+5, x2-5, y2-5, fill="black", tags=f"tile{col+1}{row+1}")
                        self.canvas.tag_bind(f"tile{col+1}{row+1}","<Button-1>", lambda e, i=col, j=row: self.get_info(e,i,j))

    def get_info(self, event, i, j):
        x1 = i * 60
        y1 = (5-j) * 60
        x2 = x1 + 60
        y2 = y1 + 60
        global click
        #discover a box of other board
        if click == 6:
            if(img[5-j][i] == 0):
                self.board[i][j] = self.canvas.create_rectangle(x1, y1, x2, y2, fill="white")
            else:
                self.board[i][j] = self.canvas.create_rectangle(x1, y1, x2, y2, fill="black")
            click = 0
            print("click counter reset")

    def get_location(self, event, i, j):
        x1 = i * 60
        y1 = (5-j) * 60
        x2 = x1 + 60
        y2 = y1 + 60
        global click
        global start
        global finish
        if click < 6 and click%2 == 0:
            start = i
            finish = j
            click = click+1
        elif click < 6 and click%2 == 1:
            if img[i][j] != img[start][finish]:
                print("information difference")
            else:
                print("equality of information")
            click = click+1
        #print (i+1, j+1)
'''
    def drawboard2(self):
        for col in range(self.n):
            color = "#ff0000"
            for row in range(self.n):
                x1 = col * 60 + 600
                y1 = (5-row) * 60
                x2 = x1 + 60
                y2 = y1 + 60
                if(visible[5-row][col] == 1):
                    self.board[row][col] = self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, tags=f"tile{col+1}{row+1}")
                    self.canvas.tag_bind(f"tile{col+1}{row+1}","<Button-1>", lambda e, i=col, j=row: self.get_location2(e,i,j))
                else:
                    if(img[5-row][col] == 0):
                        self.board[row][col] = self.canvas.create_rectangle(x1+5, y1+5, x2-5, y2-5, fill="white", tags=f"tile{col+1}{row+1}")
                        self.canvas.tag_bind(f"tile{col+1}{row+1}","<Button-1>", lambda e, i=col, j=row: self.get_info2(e,i,j))
                    else:
                        self.board[row][col] = self.canvas.create_rectangle(x1+5, y1+5, x2-5, y2-5, fill="black", tags=f"tile{col+1}{row+1}")
                        self.canvas.tag_bind(f"tile{col+1}{row+1}","<Button-1>", lambda e, i=col, j=row: self.get_info2(e,i,j))

    def get_info2(self, event, i, j):
        x1 = i * 60 + 600
        y1 = (5-j) * 60
        x2 = x1 + 60
        y2 = y1 + 60
        global click
        #discover a box of other board
        if click == 6:
            if(img[5-j][i] == 0):
                self.board[i][j] = self.canvas.create_rectangle(x1, y1, x2, y2, fill="white")
            else:
                self.board[i][j] = self.canvas.create_rectangle(x1, y1, x2, y2, fill="black")
            click = 0
            print("click counter reset")

    def get_location2(self, event, i, j):
        x1 = i * 60 + 600
        y1 = (5-j) * 60
        x2 = x1 + 60
        y2 = y1 + 60
        global click
        global start
        global finish
        if click < 6 and click%2 == 0:
            start = i
            finish = j
            click = click+1
        elif click < 6 and click%2 == 1:
            if img[i][j] != img[start][finish]:
                print("information difference")
            else:
                print("equality of information")
            click = click+1
        #print (i+1, j+1)
'''

board = Layout()
board.drawboard()
#board.drawboard2()
board.mainloop()

您需要做的是创建具有不同名称和引用的第二块板。 你只有一个self.board 如果在绘制第二块板时覆盖它,您将丢失先前存储的第一块板上的信息。

你需要第二张董事会名单。 .drawboard().get_location()方法可以同时处理两个板。

只需在第一个 go 中同时创建两个:

       ## Get a reference list for both boards.
       self.board1 = [[None for row in range(n)] for col in range(n)]
       self.board2 = [[None for row in range(n)] for col in range(n)]

    def drawboard(self):
        for col in range(self.n):
            color = "#ff0000"
            for row in range(self.n):
                x1 = col * 60
                y1 = (5-row) * 60
                x2 = x1 + 60
                y2 = y1 + 60
                if(visible[5-row][col] == 0):
                    ## Rectangle on board 1
                    self.board1[row][col] = self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, tags=f"tile1{col+1}{row+1}")
                    self.canvas.tag_bind(f"tile1{col+1}{row+1}","<Button-1>", lambda e, i=col, j=row, board=1: self.get_location(e,i,j,board))
                    
                    ## Rectangle on board 2
                    self.board2[row][col] = self.canvas.create_rectangle(x1+600, y1, x2+600, y2, fill=color, tags=f"tile2{col+1}{row+1}")
                    self.canvas.tag_bind(f"tile2{col+1}{row+1}","<Button-1>", lambda e, i=col, j=row, board=2: self.get_location(e,i,j,board))

...(and so on)

当你点击一个图块时,你将调用方法.get_location()与三个 arguments。 i、j 和董事会。 然后,您可以处理请求。

    def get_location(self, event, i, j, board):
        xshift = 0
        if board==2: xshift = 600
        x1 = i * 60 + xshift
        y1 = (5-j) * 60
        x2 = x1 + 60 + xshift
        y2 = y1 + 60

        ...

此外,我将用 class 属性替换 class 中的全局变量。 因此,您不必一直将它们定义为全局的。 只需将值存储在self.clickself.start等中。 因此,您可以在所有方法中查看和更改它们。

暂无
暂无

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

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