如何解决Tkinter,有两个独立的板子可以参考
问题可能出在注释部分。
我必须在屏幕上有两个标签。这两个棋盘必须相互作用:如果我点击一个棋盘的白色或黑色按钮,在另一个棋盘上对应的红色按钮必须分别变为白色或黑色;相反,如果你点击两个红色按钮,你必须有关于“信息内容”的信息。
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}",j=row: self.get_info(e,j))
else:
self.board[row][col] = self.canvas.create_rectangle(x1+5,fill="black",j))
def get_info(self,event,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,fill="white")
else:
self.board[i][j] = self.canvas.create_rectangle(x1,fill="black")
click = 0
print("click counter reset")
def get_location(self,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,j=row: self.get_location2(e,j=row: self.get_info2(e,j))
def get_info2(self,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,fill="black")
click = 0
print("click counter reset")
def get_location2(self,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()
方法可以设计为同时处理两个板。
只需在第一次同时创建两个:
## 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,x2+600,tags=f"tile2{col+1}{row+1}")
self.canvas.tag_bind(f"tile2{col+1}{row+1}",board=2: self.get_location(e,board))
...(and so on)
当您单击一个磁贴时,您将使用三个参数调用方法 .get_location()
。 i、j 和板。然后,您可以处理请求。
def get_location(self,event,board):
xshift = 0
if board==2: xshift = 600
x1 = i * 60 + xshift
y1 = (5-j) * 60
x2 = x1 + 60 + xshift
y2 = y1 + 60
...
此外,我会用类属性替换类中的全局变量。因此,您不必一直将它们定义为全局的。只需将值存储在 self.click
、self.start
等中。因此,您可以在所有方法中查看和更改它们。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。