【问题标题】:Get input values from a grid with several Entry widgets on Tkinter从 Tkinter 上具有多个 Entry 小部件的网格中获取输入值
【发布时间】:2020-05-18 17:43:40
【问题描述】:

我正在尝试创建一个数独求解器。此时我的网格中有条目,但我不知道如何获取用户输入的值。

我还不知道如何制作数独求解器,但我认为我首先必须找到一种方法将输入存储在一些变量中,以便以后使用它们.

所以我的问题是,如何获取已填写到条目中的值?

这是我目前的代码:

from tkinter import *

root = Tk()
root.title('Sudoku Solver')
root.geometry('500x400')

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

# Create the grid
def beg():
    global e
    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='black'
            else:
                kleur='white'
            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
            e = Entry(cells[row, column], width=4, bg='white', highlightthickness=0, fg='black', relief=SUNKEN)
            e.pack()



# Tell the button what to do
def solve():
    global e
    test = e.get()
    print(test)

# Create the buttons and give them a command
clearer = Button(root, text='Clear', command=beg)
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)

# Run it for the first time
beg()

root.mainloop()

我也尝试将 e 更改为 e[row, column] 但这给了我一个语法错误。

【问题讨论】:

  • 您必须将所有Entries 保留在列表中。您不能使用e = ... 将许多元素保留在变量中。您需要e = [] 并使用e.append()。稍后您可以使用循环 for item in e: print(e.get()) 获取值。如果您将保留在二维列表中,那么您可以使用e[row][col].get()

标签: python tkinter input


【解决方案1】:

标准规则:如果您有很多元素,则将它们保留在列表或字典中。

cells做同样的事情

创建字典

entries = {}

添加到字典

entries[(row, column)] = e

从字典中获取

def solve():
    for row in range(1, 10):
        for column in range(1, 10):
            print(row, column, entries[(row, column)].get() )

# from tkinter import * # PEP8: `import *` is not preferred
import tkinter as tk

# --- functions ---

# Create the grid
def beg():

    # remove old widgets before creating new ones
    for key, val in cells.items():
        print(key, val)
        val.destroy()


    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='black'
            else:
                kleur='white'

            cell = tk.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

            e = tk.Entry(cell, width=4, bg='white', highlightthickness=0, fg='black', relief='sunken')
            e.pack()

            entries[(row, column)] = e

# Tell the button what to do
def solve():
    for row in range(1, 10):
        for column in range(1, 10):
            print(row, column, entries[(row, column)].get() )

# --- main ---

entries = {}
cells = {}

root = tk.Tk()
root.title('Sudoku Solver')
root.geometry('500x400')

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

# Create the buttons and give them a command
clearer = tk.Button(root, text='Clear', command=beg)
solver = tk.Button(root, text='Solve', command=solve)

# Locate the buttons
clearer.grid(row=11, column=2, pady=30, columnspan=3) # I added `columnspan=3`
solver.grid(row=11, column=6, pady=30, columnspan=3) #  I added `columnspan=3`

# Run it for the first time
beg()

root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-06
    • 2018-05-02
    • 2021-08-29
    • 2019-02-26
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多