Estrobeda Estrobeda - 1 year ago 135
Python Question

Tkinter, custom widget and label updates


I want to make a custom

"Table Widget"
for Tkinter and so far it's almost as I want.

The problem I have is that I want the table to consist of
, I have only found that you have to use
in order to update the labels.

I dont really want 1000 variables for 1000 labels so I have tried to solve this issue for a week now. The only solution I've found is to use
but I dont think this is a good way to do it? I might be wrong but it feels like it has to be a way to do something like this:

myLabel = Label(root, text='myText')
myLabel.setText('new text')

The current widget code looks like this
(This is my first custom widget so if this is horribly wrong or wrong in any way, please tell me)


A TABLE WIDGET Extension for Tkinter
#Python 3.X
import tkinter #this is quite annoyng, without it (tkinter) wont be available....
from tkinter import * # even with this
from tkinter import ttk
from tkinter.ttk import *

except ImportError:
#python 2.X
import tkinter
from Tkinter import *
from Tkinter import ttk
from Tkinter.ttk import *

class ETable(tkinter.Frame):
def __init__(self, parent, *args, **kwargs):
tkinter.Frame.__init__(self, parent)
self.init_table(parent, *args, **kwargs)

def init_table(self, parent, *args, **kwargs):
row_settings = {
'anchor': CENTER,
'background': '#C5FFFF',
'bitmap': None,
'borderwidth': 5,
'compound': None,
'cursor': None,
'font': None,
'foreground': '#000000',
'image': None,
'relief': FLAT,
'rows': 1,
'state': NORMAL,
'text': None,
'textvariable': None,
'underline': -1,
'width': 20,

for kwarg in kwargs:
if kwarg in row_settings:
row_settings[kwarg] = kwargs[kwarg]

self.table_rows = self.init_row(parent, *args, **row_settings)

def init_row(self, parent, *args, **kwargs):
text_list = kwargs.pop('text')
row_list = []

cols = []
rows = kwargs.pop('rows')

for row in range(rows):
for col in range(len(text_list)):
tempLabel = Label(parent, text = text_list[col], *args, **kwargs)
tempLabel.grid(row=row, column=col)

return row_list


I want to be able to do something like this

table.getRow[0][0].setText('new text') #This would change col 1 in row 1 to "new text"

Bonus Question

(please tell me if I should make a new question for this)

As I said, I want to make a
"Table Widget"
for Tkinter but I also want to add behaviours to the table, IE when a user clicks on a row, I want the row to expand
much like a
"Dropdown Menu"
but it will only close when it looses focus, the goal here is to add
"Entry or Text boxes"
in order to edit the columns.

The Question here is, is this possible with Tkinter?

Answer Source

I have only found that you have to use "StringVar()'s" in order to update the labels.

This is not a true statement. You can update the attributes of any widget with the config / configure method. Starting with your first example, it would look like this:

myLabel = Label(root, text='myText')
myLabel.configure(text='new text')

And, in your final example:

table.getRow[0][0].configure(text='new text') 

As for the bonus question, yes, it's possible.You can bind any code you want to a label just as you can any other widget, and in addition to bindings on keys and mouse buttons, you can bind on gaining and losing focus.