我正在Tkinter Python上创建一个Sudoku GUI。几乎完成但我想要发生的是当你点击一个按钮(9x9网格中的一个正方形)时,它是一个0(网格中的一个数字,以前没有设置在开头(一个空的间隙)我已经指定为红色0))它增加1.所以如果你点击这4次它就变成了数字4.(你可能想要主要参考----主要代码---在底部)
from tkinter import *
import random
frame=Tk()
menu=Menu(frame)
file=Menu(menu)
file.add_command(label="Exit", command=frame.quit)
file.add_command(label="EASY LEVEL", command=lambda:easyLvl())
file.add_command(label="EASY LEVEL SOLVED", command=lambda:easyLvlSolved())
file.add_command(label="HARD LEVEL", command=lambda:hardLvl())
file.add_command(label="EASY LEVEL SOLVED", command=lambda:hardLvlSolved())
menu.add_cascade(label="Choose Level (Easy or Hard)", menu=file)
frame.config(menu=menu)
listofnumbers0=[0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0]
hardUnsolved=[8,0,0,0,0,0,0,0,0,
0,0,3,6,0,0,0,0,0,
0,7,0,0,9,0,2,0,0,
0,5,0,0,0,7,0,0,0,
0,0,0,0,4,5,7,0,0,
0,0,0,1,0,0,0,3,0,
0,0,1,0,0,0,0,6,8,
0,0,8,5,0,0,0,1,0,
0,9,0,0,0,0,4,0,0]
hardSolved=[8, 1, 2, 7, 5, 3, 6, 4, 9,
9, 4, 3, 6, 8, 2, 1, 7, 5,
6, 7, 5, 4, 9, 1, 2, 8, 3,
1, 5, 4, 2, 3, 7, 8, 9, 6,
3, 6, 9, 8, 4, 5, 7, 2, 1,
2, 8, 7, 1, 6, 9, 5, 3, 4,
5, 2, 1, 9, 7, 4, 3, 6, 8,
4, 3, 8, 5, 2, 6, 9, 1, 7,
7, 9, 6, 3, 1, 8, 4, 5, 2]
easyUnsolved=[5,1,7,6,0,0,0,3,4,
2,8,9,0,0,4,0,0,0,
3,4,6,2,0,5,0,9,0,
6,0,2,0,0,0,0,1,0,
0,3,8,0,0,6,0,4,7,
0,0,0,0,0,0,0,0,0,
0,9,0,0,0,0,0,7,8,
7,0,3,4,0,0,5,6,0,
0,0,0,0,0,0,0,0,0]
easySolved=[5,1,7,6,9,8,2,3,4,
2,8,9,1,3,4,7,5,6,
3,4,6,2,7,5,8,9,1,
6,7,2,8,4,9,3,1,5,
1,3,8,5,2,6,9,4,7,
9,5,4,7,1,3,6,8,2,
4,9,5,3,6,2,1,7,8,
7,2,3,4,8,1,5,6,9,
8,6,1,9,5,7,4,2,3]
i=0
q=0
thelist=[listofnumbers0,easyUnsolved, easySolved,hardUnsolved, hardSolved]
def easyLvl():
global q
q=1
createGrid()
def easyLvlSolved():
global q
q=2
createGrid()
def hardLvl():
global q
q=3
createGrid()
def hardLvlSolved():
global q
q=4
createGrid()
def btnCommand(x):
if x==0:
x=x+1
colourTxt="black"
#-----------------------------MAIN CODE------------------
def createGrid():
for rowindex in range (9):
for colindex in range (9):
if (rowindex in (0,1,2,6,7,8) and colindex in (3,4,5) or \
(rowindex in (3,4,5) and colindex in (0,1,2,6,7,8))):
colour="light blue"
else:
colour="white"
global i
x=thelist[q][i]
i=i+1
if i==81:
i=0
if x==0:
colourTxt="red"
else:
colourTxt="black"
btn=Button(frame, width=8, height=4, bg=colour, text=x, fg=colourTxt, command=lambda:btnCommand(x))
btn.grid(row=rowindex, column=colindex, sticky=N+S+E+W)
btn.grid(row=rowindex, column=colindex, sticky=N+S+E+W)
createGrid()
frame.mainloop()
答案 0 :(得分:0)
您必须在按钮回调中修复lambda值,这是我添加到代码中的主要更改。所有其他编辑主要是为了使代码更清晰,并删除所有不必要的部分。
回到主页消息ist,如果你没有修复传递给lambda函数的值,那么你基本上总是使用Button(command=...)
的回调函数的最后一个迭代器值。添加row=rowindex,...
会强制Python在创建按钮时回顾/保存值。
import numpy as np
hardUnsolved=np.array([[8,0,0,0,0,0,0,0,0],...])
def btnCommand(row, col, x):
x += 1
hardUnsolved[row][col] = x
createGrid()
#-----------------------------MAIN CODE------------------
def createGrid():
for rowindex in range (9):
for colindex in range (9):
if ((rowindex in (0,1,2,6,7,8) and colindex in (3,4,5)) or \
rowindex in (3,4,5) and colindex in (0,1,2,6,7,8))):
colour='blue'
else:
colour='white'
x=hardUnsolved[rowindex][colindex]
if x==0:
colourTxt='red'
else:
colourTxt='black'
btn=Button(frame, width=4, height=3, bg=colour, text=x, fg=colourTxt,
command=lambda row=rowindex, col=colindex, x=x: btnCommand(row, col, x))
btn.grid(row=rowindex, column=colindex, sticky=N+S+E+W)
btn.grid(row=rowindex, column=colindex, sticky=N+S+E+W)
mainloop()
createGrid()
编辑:我还导入了numpy
,以便为您的网格分配值更直观,更干净。
答案 1 :(得分:0)
在下面的代码中,按钮循环显示小数字,除了0,基本上是数字,对于widget
的文本:
import tkinter as tk
def cycle(widget):
widget['text'] = (widget['text'] % 9) + 1
root = tk.Tk()
btn = tk.Button(root, text=1)
btn['command'] = lambda widget=btn: cycle(widget)
btn.pack()
root.mainloop()