Description
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 "Labels"
, I have only found that you have to use "StringVar()'s"
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 "Dicts"
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.pack()
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)
Source
"""
Description:
A TABLE WIDGET Extension for Tkinter
"""
try:
#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)
cols.append(tempLabel)
row_list.append(cols)
return row_list
Goal
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 "Downwards"
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?
See Question&Answers more detail:
os