Python Forum
[Tkinter] apply command on every list element
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] apply command on every list element
#1
Hello,
I'm totally new in programming in python.

I'm planning to write a sudoku solver in python. I have a frame with 81 entries on it and I want to read the values of each entry with '.get()'.
Therefore I got a list with all entries and I want to apply the command '.get()' to each element of the list. How that could be done?

Thanks a lot for your help!!

Greetings, flash77

import tkinter as tk
fenster = tk.Tk()
fenster.geometry("500x540")
fenster.resizable(width=0, height=0)
l=tk.Label(fenster, text="Sudoku Solver")
l["font"]="Arial"
l.place(x=240,y=0)

def tboxErzeugen():
    for i in range(0, 360, 40):
        for j in range(0, 360, 40):
            t= tk.Entry(fenster, font=("Times New Roman",12))
            t.place(x=i+70, y=j+80, width=40, height=40)

def test(a):
    if type(a)==tk.Entry:
        return True
    else:
        return False

tboxErzeugen()
b=tk.Button(fenster, text="einlesen", command = test)
b.place(x=50,y=450)

w = []
w = fenster.winfo_children()
u = filter(test,w)
e = []
for i in u:
    print("True:",i)
    e.append(i)
print(e)

fenster.mainloop()
Reply
#2
I would use an IntVar instead of trying to talk directly to the Entry objects.
squares = []
for r in range(9):
    y = 80 + r * 40
    for c in range(9):
        var = IntVar()
        squares.append(var)
        x = 70 + c * 40
            t= tk.Entry(fenster, font=("Times New Roman",12), textvars=var)
            t.place(x=x, y=y, width=40, height=40)
Then you could use square[index].set(value) or square[index].get()
Reply
#3
Dear deanhystad!

Thanks a lot for your answer!!
Reply
#4
Hello,

Unfortenately I need further help...

First the squares are created by the function SquareCreate().

Then I want to fill in the values of the Sudoku-Puzzle.

After that I click the button to read in the values.

I don't know how to adress the right square by using i and j in the function squareread().

I have got little experience in programming but Python is a new language for me.

Please be so kind and help me, because I'm new to Python.

Greetings, flash77

import tkinter as tk
from tkinter import Entry, IntVar, Tk
fenster = tk.Tk()

fenster.geometry("500x540")
fenster.resizable(width=0, height=0)
l=tk.Label(fenster, text="Sudoku Solver")
l["font"]="Arial"
l.place(x=240,y=0)

def SquareCreate(): 
    for i in range(0, 9):
        for j in range(0,9):
            data = IntVar()
            t = tk.Entry(fenster, textvariable=data, font=("Times New Roman",12))
            t.place(x=i*40+70, y=j*40+80, width=40, height=40)

Square = []            
def SquareRead(): 
    for i in range(0, 9):
        for j in range(0,9):
            data = IntVar()
            Square.append(data)
            
print(Square)

#Hauptprogramm
SquareCreate()
b=tk.Button(fenster, text="einlesen", command = SquareRead)
b.place(x=50,y=450)

fenster.mainloop()
Reply
#5
You are making an IntVar that you bind to the entry. Then you create a different IntVar and put in in a list. There is no relationship between these IntVars. Calling Square[1].set(5) does not do anything to any of your entries because it is not bound to any of those entries. The IntVar you put in the list Square must be THE SAME intvar that you bind to Entry t.
Reply
#6
Hello,
thanks a lot for your answer!!

Do you mean it this way?

import tkinter as tk
from tkinter import Entry, IntVar, Tk
fenster = tk.Tk()

fenster.geometry("500x540")
fenster.resizable(width=0, height=0)
l=tk.Label(fenster, text="Sudoku Solver")
l["font"]="Arial"
l.place(x=240,y=0)

Square = []
def SquareCreate(): 
    for i in range(0, 9):
        for j in range(0,9):
            data = IntVar()
            t = tk.Entry(fenster, textvariable=data, font=("Times New Roman",12))
            t.place(x=i*40+70, y=j*40+80, width=40, height=40)
            Square.append(data)

def quit():
    fenster.destroy()
            
#Hauptprogramm
SquareCreate()
b=tk.Button(fenster, text="schließen", command = quit)
b.place(x=50,y=450)
print(Square)
fenster.mainloop()
But I'm not sure how to place the get() in SquareCreate().

Thanks for your patience!
Reply
#7
Make a tiny change to your program to initialize each Entry to the index of the Intvar in Square (line 18). Run the program. See those numbers, they are the numbers you use when you want to work with that square. To convert row/column to these values use the equation:

index = column * 9 + row.

If I want to get the value in row 5, column 7 I call Square[68].get().
If I want to set row 8, column 2 = 5 I call Square[26].set(5)
import tkinter as tk
from tkinter import Entry, IntVar, Tk
fenster = tk.Tk()
 
fenster.geometry("500x540")
fenster.resizable(width=0, height=0)
l=tk.Label(fenster, text="Sudoku Solver")
l["font"]="Arial"
l.place(x=240,y=0)
 
Square = []
def SquareCreate(): 
    for i in range(0, 9):
        for j in range(0,9):
            data = IntVar()
            t = tk.Entry(fenster, textvariable=data, font=("Times New Roman",12))
            t.place(x=i*40+70, y=j*40+80, width=40, height=40)
            data.set(len(Square))
            Square.append(data)
 
def quit():
    fenster.destroy()
             
#Hauptprogramm
SquareCreate()
b=tk.Button(fenster, text="schließen", command = quit)
b.place(x=50,y=450)
print(Square)
fenster.mainloop()
Reply
#8
Dear deanhystad!

Thanks a lot for your answer!!

Now I'm able to read in the values of the entries - many thanks for your patient help!!

I am geared to some python-Code I found in the web.

I will set the question to "solved".

Thank you very much again for your well versed help!!

Have a nice evening!

flash77
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020