How to add widget to and reference from list in python?

Question:

I want to add widget to list and reference thereafter at runtime but i get error:

My code:

import tkinter as tk
from tkinter import ttk

p = tk.Tk()

1. p.geometry('600x350')

widget_list = []

for i in range(10):
    button = ttk.Button(p, text=f"Button #{i+1}").grid(column=0, row=i)
    widget_list.append(button)

for widget in widget_list:
    print(widget['text'])
    # widget['text']

p.mainloop()

error:

Traceback (most recent call last):
File "D:daponeDocumentssmart countstcstestgui.py", line 15, in
print(widget[‘text’])
~~~~~~^^^^^^^^
TypeError: ‘NoneType’ object is not subscriptable

what am i doing wrong? Thanks

I’ve tried the above but not working. I’m using python 3.11.0

Asked By: TryCatchFinally

||

Answers:

The problem is that the grid(...) method does not return a button.

This code should work:

import tkinter as tk
from tkinter import ttk

p = tk.Tk()

p.geometry('600x350')

widget_list = []

for i in range(10):
    button = ttk.Button(p, text=f"Button #{i+1}")
    button.grid(column=0, row=i)
    widget_list.append(button)

for widget in widget_list:
    print(widget['text'])
    # widget['text']

p.mainloop()

The elements in your list are NoneType because the grid method does not return a value (meaning any variable you assign to its return value is None). But when you assign a variable to the return value of ttk.Button the variable is bound to a widget.

The only part of your code I changed is the contents of the for loop. It works fine when I test it.

% python buttons.py
Button #1
Button #2
Button #3
Button #4
Button #5
Button #6
Button #7
Button #8
Button #9
Button #10

A function in Python can return a value, just like a method in Java or a function in C. But a function in Python does not have to return a value. The same is true in Java and C. A Java method can have a void return type and a C function can have a void return type.

The grid method has a return type of None.

The ttk.Button constructor returns a widget.

That explains why the line button = ttk.Button(p, text=f"Button #{i+1}") gets it working. You can add the button to the grid in the next line of code. When you call the grid method, you are adding the button widget to the container using a grid layout.

You can get documentation on these tkinter functions with the following code (using Python in interactive mode).

% python
>>> import tkinter as tk
>>> from tkinter import ttk
>>> help(ttk.Button)
>>> help(ttk.Button.grid)

It also helps to include the code for a simple hello world application using the tkinter framework.

import tkinter

window = tkinter.Tk()
window.title('Hello World')
window.geometry('700x700')

label = tkinter.Label(window, text='Hello Tk!', bg='yellow', fg='blue', font=('Times New Roman', 30))
label.pack()

window.mainloop()

Tk means "toolkit". It is a graphical toolkit that was originally written for Tcl and then ported to Python as tkinter. In other words, it is a Python binding for the Tcl/Tk framework.

tkinter means "Tk interface". It is the Python interface to the Tk framework.

If Python were to create a binding for the Java Swing framework, it would be the same situation. The Swing framework is popular in Java and the Tk framework is popular in Tcl. Python chose to adopt the Tk framework from the Tcl language.

Answered By: ktm5124
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.