2

我将如何在 Tkinter 上创建一个可以从键盘输入条目的表格?

到目前为止,我已经创建了一个 Tkinter 框架。

import Tkinter as tk
import numpy as np
import scipy as sp


class app(tk.Frame):
def __init__(self, master = None):
    tk.Frame.__init__(self, master)
    self.grid(ipadx = 300, ipady = 300)


prog = app()
prog.master.title('Sudoku')
prog.mainloop()
4

1 回答 1

1

这适用于 python 2.7:

from Tkinter import *
from string import ascii_lowercase

class app(Frame):
    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.entries = {}
        self.tableheight = 9
        self.tablewidth = 9
        counter = 0
        for row in xrange(self.tableheight):
            for column in xrange(self.tablewidth):
                self.entries[counter] = Entry(self, width=5)
                self.entries[counter].grid(row=row, column=column)
                counter += 1

prog = app()
prog.master.title('Sudoku')
prog.mainloop()

要访问一个条目(填充它或获取它的值),请在条目字典中找到它的索引。例如:

self.entries[15].insert(0, '15') 
于 2013-08-07T14:38:05.097 回答