我想制作带有条目的9x9网格,因为我想制作数独求解器,但找不到有效的方法。我想这样做,但不认为这是最好的方法。
from tkinter import *
main = Tk()
a1 = Entry(main)
a2 = Entry(main)
a3 = Entry(main)
a1.place(x=0, y=0, width=40)
a2.place(x=40, y=0, width = 40)
a3.place(x=80, y=0, width = 40)
main.mainloop()
答案 0 :(得分:0)
使用2个嵌套的for循环,将Entry小部件保存在列表中,以便以后使用。
from tkinter import *
main = Tk()
entries = []
for a in range(0, 120, 40):
for b in range(0, 120, 40):
temp = Entry(main)
temp.place(x=a, y=b, width=40, height=40)
entries.append(temp)
main.mainloop()
答案 1 :(得分:0)
您可以使用自定义类和字典来存储盒子数据;将其保存为test.py,查看结果图片
import tkinter as tk
root = tk.Tk()
class Entry(tk.Entry):
def __init__(self, master, x, y):
tk.Entry.__init__(self, master)
self.data = tk.IntVar()
self.textvariable = self.data
self.grid(column=x, row=y)
self.config(width=2)
self.data.trace('w', self.edit_entry)
def edit_entry(self, *args):
self.data.set(self.get())
boxes = {}
box_name = 1
for x in range(9):
for y in range(9):
boxes[box_name] = Entry(root, y, x)
box_name += 1
def print_grid():
box_name = 1
for x in range(9):
for y in range(9):
print(boxes[box_name].get(), end=',')
box_name += 1
print('')