如何使用Tkinter请求数独游戏输入

2024-04-18 08:58:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Tkinter创建一个数独解算器。我希望用户能够填写一些随机数,我的程序应该给出一个解决方案

我遇到的第一个问题是我需要用户的输入。我试图通过为每一行和每一列组合创建一个条目来实现这一点,但这不起作用。它只是给了我一些看起来很奇怪的列,彼此分开

我应该如何要求用户输入数字,以便在所有框中都可以填写数字

 from tkinter import *


root = Tk()
root.title('Sudoku Solver')
root.geometry('460x550')

mylabel = Label(root, text='Fill in the numbers and click solve').grid(row=0, column=0,
                                                                                                    columnspan=10)

# Create the grid
def beg():
    cells = {}
    for row in range(1, 10):
        for column in range(1, 10):
            if ((row in (1,2,3,7,8,9) and column in (4,5,6)) or (row in (4,5,6) and column in (1,2,3,7,8,9))):
                kleur='green'
            else:
                kleur='orange'
            cell = Frame(root, bg='white', highlightbackground=kleur,
                         highlightcolor=kleur, highlightthickness=2,
                         width=50, height=50,  padx=3,  pady=3, background='black')
            cell.grid(row=row, column=column)
            cells[(row, column)] = cell
            for roww, columnn in cells:
                e = Entry(cells[roww, columnn])
                e.pack()


beg()

# Create the functions for buttons
def clear():
    return


def solve():
    return


# Create the buttons
clearer = Button(root, text='Clear', command=clear)
solver = Button(root, text='Solve', command=solve)

# Locate the buttons
clearer.grid(row=11, column=3, pady=30)
solver.grid(row=11, column=7, pady=30)


root.mainloop()

Tags: andthetext用户infordefcreate
1条回答
网友
1楼 · 发布于 2024-04-18 08:58:58

看起来你创建了一个不需要的额外循环,我取出了一个,现在看起来很好,除了入口宽度。 我不确定我是否把行和列弄混了。但它是正方形的,所以它应该以任何一种方式工作:

from tkinter import *


root = Tk()
root.title('Sudoku Solver')
root.geometry('460x550')

mylabel = Label(root, text='Fill in the numbers and click solve').grid(row=0, column=0,columnspan=10)

# Create the grid
def beg():
    cells = {}
    for row in range(1, 10):
        for column in range(1, 10):
            if ((row in (1,2,3,7,8,9) and column in (4,5,6)) or (row in (4,5,6) and column in (1,2,3,7,8,9))):
                kleur='green'
            else:
                kleur='orange'
            cell = Frame(root, bg='white', highlightbackground=kleur,
                         highlightcolor=kleur, highlightthickness=2,
                         width=50, height=50,  padx=3,  pady=3, background='black')
            cell.grid(row=row, column=column)
            cells[(row, column)] = cell
            # for roww, columnn in cells:
            e = Entry(cells[row, column])
            e.pack()



beg()

# Create the functions for buttons
def clear():
    return


def solve():
    return


# Create the buttons
clearer = Button(root, text='Clear', command=clear)
solver = Button(root, text='Solve', command=solve)

# Locate the buttons
clearer.grid(row=11, column=3, pady=30)
solver.grid(row=11, column=7, pady=30)


root.mainloop()

额外解释一下: 前两个循环(81次)的组合也将此循环称为81次:

for roww, columnn in cells:
    e = Entry(cells[roww, columnn])
    e.pack()

用81个输入字段填充所有81个帧。这就是它看起来疯狂的原因

相关问题 更多 >