【发布时间】:2021-05-04 23:09:49
【问题描述】:
可能问题出在注释部分。
我必须在屏幕上有两个标签。这两个棋盘必须相互交互:如果我单击一个棋盘的白色或黑色按钮,则在另一个棋盘上相应的红色按钮必须分别变为白色或黑色;相反,如果您单击两个红色按钮,则必须具有有关“信息内容”的信息。
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()
【问题讨论】:
-
如果您将第二块板命名为与第一块完全相同,则对第一个小部件的引用将丢失,所有更改仅影响第二块。你甚至不能再删除第一个了。你应该给他们单独的变量名。您不需要两个
.drawboard()方法,您只需要其中一个,但它应该定义两个板。 -
我不想浪费太多时间,但你可以给我一个小例子(甚至独立于我的代码)@MartinWettstein
标签: python tkinter tkinter-canvas tkinter-layout