【发布时间】:2018-01-15 19:45:27
【问题描述】:
我正在 Tkinter Python 上创建数独 GUI。几乎完成但我想要发生的是,当您单击一个按钮(9x9 网格中的单个正方形)时,它是一个 0(网格中的一个数字,以前没有在开始时设置(一个空白的间隙,我'已分配为红色0s)) 增加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()
【问题讨论】:
-
第 10 行的错字:字符串应该是硬级别解决的。
-
干杯。现在改变了
-
代码太多了。请将其缩减为minimal reproducible example。
标签: python button tkinter sudoku