【问题标题】:Sudoku 9x9 GUI Grid Python/Tkinter Buttons数独 9x9 GUI 网格 Python/Tkinter 按钮
【发布时间】: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


【解决方案1】:

在下面的代码中,按钮循环显示除 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()

【讨论】:

    【解决方案2】:

    您必须在按钮回调中修复您的 lambda 值,这是我添加到您的代码中的主要更改。其他所有的修改主要是为了让代码更清晰,去掉所有不必要的部分。

    这里有一条消息,如果你不修复传递给你的 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,以便将值分配给您的网格更加直观和简洁。

    【讨论】:

    • 在第 x=hardUnsolved[rowindex][colindex] 行收到错误,指出 int 对象不可下标?
    • 你必须以import numpy as np开头并将你的数独定义为np.array([[...],[...],...])
    • 请考虑关闭线程,因为您的原始问题已得到解答。
    猜你喜欢
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    相关资源
    最近更新 更多