Python Forum
[Tkinter] Python Newbie
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Python Newbie
#1
Hi guys,

I hope you can helpppppp.....

I have a program which is supposed to get the new values of 9 variables.

It initializes [init()] by arranging the numbers similar to a spreadsheet, and putting varaibles in the "textvariable" as var00, var01, var02 etc

Then it give values to each by attempting to use the .get method of the Control variables.

But my program does not work.
Thanks
Marty

import tkinter as tk
from tkinter import *

root = tk.Tk()

def give_values():
    # row 0
    var00 = StringVar().get()
    var01 = StringVar().get()
    var02 = StringVar().get()
    # row 1
    var10 = StringVar().get()
    var11 = StringVar().get()
    var12 = StringVar().get()
    # row 2
    var20 = StringVar().get()
    var21 = StringVar().get()
    var22 = StringVar().get()
    v = (var00, var01, var02, var10, var11, var12, var20, var21, var22)
    print(v)

def init():
    for i in range(3):
        for j in range(3):
            frame = tk.Frame(
                master=root,
                relief=tk.RAISED,
                width=2,
                borderwidth=1
            )
            frame.grid(row=i, column=j)
            entry = tk.Entry(master=frame,
                             text=f"Row {i}\nColumn {j}",
                             textvariable="var" + str(i) + str(j))
            entry.pack()


give_values()
init()
give_values()
# b1 = Button(root, text="Done", width=10).grid(row=0, column=10, rowspan=10)  # command=done)
b2 = Button(root, text="Enter", width=10, command=give_values).grid(row=1, column=10, rowspan=10)  # command=enter())

root.mainloop()
Reply
#2
Thanks for changing the location of my post to the correct one! I am ready to throw in the towel -- i've tried everything that I know -- I'm using python 3.7.3 Is there anything else I can add?

Thanks marty
Reply
#3
A couple of problems here. You are creating new StringVars every time you call give_values so there is no way these can be bound to an Entry. Even if you only created the StringVar's once, their attribute names (varXX) do not exist outside the function give_values, so you would have no way to get these variables and bind them to the entries. Finally, you cannot set textvariable="var00" and expect the Entry constructor to figure out which namespace to use to search for attribute "var00" and get a StringVar.

import tkinter as tk
from tkinter import *
 
root = tk.Tk()
variables = []

def give_values():
    return [var.get() for var in variables] # Makes a list calling get for each StringVar in variables
 
def init():
    for i in range(3):
        for j in range(3):
            frame = tk.Frame(
                master=root,
                relief=tk.RAISED,
                width=2,
                borderwidth=1
            )
            frame.grid(row=i, column=j)
            var = StringVar()
            variables.append(var)
            entry = tk.Entry(master=frame,
                             text=f"Row {i}\nColumn {j}",
                             textvariable=var)
            entry.pack()
 
 
init()
# b1 = Button(root, text="Done", width=10).grid(row=0, column=10, rowspan=10)  # command=done)
b2 = Button(root, text="Enter", width=10, command=give_values).grid(row=1, column=10, rowspan=10)  # command=enter())
 
root.mainloop()
Reply
#4
that's great and i am part of the way there -- I really appreciate that! I still have 2 questions:

1) I really didn't understand StringVar, and I am not sure that really do now!! Perhaps you can expand on it!

2) Another question --> so now I've got a list of <tkinter.StringVar object at 0x00F68290>, ... etc how does this translate into the value for the variable? I have tried all sorts of machinations of [variables] and can't seem to get any satifaction.

Thanks,
Marty
Reply
#5
this is old, bit outdated tutorial on tkinter, but still one of the best:
https://anzeljg.github.io/rin2/book2/240...index.html
this is the part about control variables:
https://anzeljg.github.io/rin2/book2/240...ables.html
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
A short example using most of your provided code:
import tkinter as tk
from tkinter import *
  
root = tk.Tk()
variables = []
 
def increment():
    for i in range(len(variables)-1):
        variables[i].set(variables[i+1].get())
    variables[-1].set(variables[-1].get() + 1)
    
def init():
    for i in range(3):
        for j in range(3):
            frame = tk.Frame(master=root, relief=tk.RAISED)
            frame.grid(row=i, column=j)
            var = IntVar()
            variables.append(var)
            entry = tk.Entry(master=frame, width=3, textvariable=var)
            entry.pack()

    b2 = Button(root, text="Increment", command=increment) \
         .grid(row=3, column=0, columnspan=3)

init()
root.mainloop()
Reply
#7
Hey that really helps!!
I've just discovered how line 19 related to the "Increment" method (by the line directly above it)
and I have to look at carefully to discover some other facts...
Thanks,

Marty
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] newbie with python and tkinter pat 8 5,495 Jun-26-2018, 07:43 PM
Last Post: pat

Forum Jump:

User Panel Messages

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