如何用Tkinter要求数独游戏输入?

问题描述 投票:-1回答:1

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

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

我应该如何要求用户输入数字,以便在所有的框中都能填入一个数字?

 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()
python tkinter input sudoku
1个回答
0
投票

似乎你创建了一个不需要的额外循环,我把一个循环去掉了,现在看起来很好,除了输入宽度。我不确定是不是我把行和列搞混了。但它是一个正方形,所以无论哪种方式都应该是可行的。

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个帧。这就是让它看起来很疯狂的原因。

© www.soinside.com 2019 - 2024. All rights reserved.