如何使用 Tkinter 请求数独游戏输入
How to ask for Sudoku game input with Tkinter
我想用 Tkinter 创建一个数独求解器。我希望用户能够填写一些 运行dom 数字,我的程序应该给出解决方案。
我 运行 遇到的第一个问题是我需要用户的输入。我尝试通过为每个行和列组合创建一个条目来做到这一点,但这不起作用。它只是给了我一些看起来很奇怪的列,它们彼此分开。
我应该如何向用户询问数字,以便在所有框中都可以填写一个数字?
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()
您似乎创建了一个不需要的额外循环,我去掉了一个,现在看起来不错,除了入口宽度。
我不确定我是否把行和列搞混了。但它是一个正方形,所以它应该以任何一种方式工作:
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 个帧。这就是让它看起来很疯狂的原因。
我想用 Tkinter 创建一个数独求解器。我希望用户能够填写一些 运行dom 数字,我的程序应该给出解决方案。
我 运行 遇到的第一个问题是我需要用户的输入。我尝试通过为每个行和列组合创建一个条目来做到这一点,但这不起作用。它只是给了我一些看起来很奇怪的列,它们彼此分开。
我应该如何向用户询问数字,以便在所有框中都可以填写一个数字?
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()
您似乎创建了一个不需要的额外循环,我去掉了一个,现在看起来不错,除了入口宽度。 我不确定我是否把行和列搞混了。但它是一个正方形,所以它应该以任何一种方式工作:
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 个帧。这就是让它看起来很疯狂的原因。