Python Forum
[Tkinter] How display matrix on canvas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How display matrix on canvas
#8
(Mar-15-2021, 03:14 AM)deanhystad Wrote: After playing with the table a bit I found I do not like using __setitem__() and would rather specify configuration options by providing them to __init__() or using configure(). This Table is easier to work with. I also didn't like having a list of Entry widgets and a list of StringVars, so I made a new class that is a Entry widget with a StringVar.
import tkinter as tk

def popkeys(dictionary, *keys):
    """Pop keys from dictionary.  Return values in list"""
    return [dictionary.pop(key) if key in dictionary else None for key in keys]

class EntryCell(tk.Entry):
    """tk.Entry widget with a StringVar"""
    def __init__(self, *args, **kvargs):
        self.var = kvargs.get('textvariable', False)
        if not self.var:
            self.var = tk.StringVar()
            kvargs['textvariable'] = self.var
        super().__init__(*args, **kvargs)

    def get(self):
        """Return text value"""
        return self.var.get()

    def set(self, value):
        """Set text value"""
        self.var.set(value)

class Table(tk.Frame):
    """2D matrix of Entry widgets"""
    def __init__(self, parent, rows=None, columns=None, data=None, **kwargs):
        super().__init__(parent)
        if data is not None:
            rows = len(data)
            columns = len(data[0])
        elif not rows and columns:
            raise TypeError('__init__() missing required rows and columns or data argument')
        self.rows = rows
        self.columns = columns
        self.cells = []
        for row in range(rows): 
            for col in range(columns):
                self.cells.append(EntryCell(self))
                self.cells[-1].grid(row=row, column=col)
                if data:
                    self.cells[-1].set(data[row][col])
        self.configure(**kwargs)

    def configure(self, **kwargs):
        """Set configure options.  Adds fg, font, justify and columnwidth
        to Frame's config options
        """
        row, col, bg, fg, font, justify, width = \
                popkeys(kwargs, 'row', 'column', 'bg', 'fg', 'font', 'justify', 'columnwidth')

        for cell in self.cell(row, col):
            if bg is not None: cell.configure(bg=bg)
            if fg is not None: cell.configure(fg=fg)
            if font is not None: cell.configure(font=font)
            if justify is not None: cell.configure(justify=justify)
            if width is not None: cell.configure(width=width)
        if bg is not None: kwargs['bg'] = bg
        if kwargs:
            super().configure(**kwargs)

    def cell(self, row=None, column=None):
        """Get cell(s).  To get all cells in a row, leave out column.
        To get all cells in a column, leave out row.  To get all cells
        leave out row and column.
        """
        if row is None and column is None:
            return self.cells
        elif row is None:
            return [self.cell(row, column) for row in range(self.rows)]
        elif column is None:
            return [self.cell(row, column) for column in range(self.columns)]
        return self.cells[row * self.columns + column]

table_values = [
    ('Parameter', 'Value'),
    ('Temp.Strand', 10),
    ('Temp. Kamer', 18),
    ('Temp. Keuken', 20),
    ('Temp. Zee', 20),
    ('Luchtdruk', 1020.520),
    ('Luchtdruk Max', 1020.520),
    ('Datum', '22-01-2020 10:30'),
    ('Luchtdruk Min', 1020.520),
    ('Datum', '22-01-2020 10:30')
]

font = ('Arial', 16, 'bold')

root = tk.Tk()
tk.Label(root, text='This is a table', font=font).grid(row=0, column=0)
 
table = Table(root, data=table_values, font=font, bg='black', fg='white', bd=10, columnwidth=15)
table.configure(column=0, fg='green')
table.configure(column=1, justify=tk.RIGHT)
table.configure(row=0, fg='Blue', justify=tk.CENTER)
table.grid(row=1, column=0, padx=10, pady=10)
 
root.mainloop()
I set up the configure method so you can apply configuration options to a range of cells instead of all cells. Just specify row or column in the configure parameters and it will apply the settings to cells in only that row or column.

Thank you very much. You are a great help and learned a bit more on Python.
Unfortunately if have a bit of an issue which I can't figure out.
Now that the table is presented on the screen, I can't figure out how to place an image on, let's say, the richt side from the table. Location 100,100, see the Tk.Label....what am I doing wrong here...
Thanks again for your help!!!!


root = tk.Tk()
root.attributes("-fullscreen",True)

root.configure(bg='black')
tk.Label(root, text='This is a table', font=font).grid(row=0, column=0)

table = Table(root, data=table_values, font=font, bg='black', fg='white', bd=10, columnwidth=15)
table.configure(column=0, fg='green')
table.configure(column=1, justify=tk.RIGHT)
table.configure(row=0, fg='Blue', justify=tk.CENTER)
table.grid(row=1, column=0, padx=10, pady=10)
tk.Label (root,image="image.gif" ). grid(row=100,column=100)

root.mainloop()
Reply


Messages In This Thread
How display matrix on canvas - by eedenj - Mar-13-2021, 11:53 AM
RE: How display matrix on canvas - by BashBedlam - Mar-13-2021, 02:04 PM
RE: How display matrix on canvas - by deanhystad - Mar-13-2021, 02:25 PM
RE: How display matrix on canvas - by eedenj - Mar-13-2021, 04:14 PM
RE: How display matrix on canvas - by deanhystad - Mar-14-2021, 07:34 AM
RE: How display matrix on canvas - by eedenj - Mar-14-2021, 04:48 PM
RE: How display matrix on canvas - by deanhystad - Mar-15-2021, 03:14 AM
RE: How display matrix on canvas - by eedenj - Mar-15-2021, 01:37 PM
RE: How display matrix on canvas - by deanhystad - Mar-15-2021, 06:34 PM
RE: How display matrix on canvas - by eedenj - Mar-17-2021, 03:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 25,235 Jun-04-2019, 05:05 AM
Last Post: Gupi
  Display and update the label text which display the serial value jenkins43 5 9,234 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  Display more than one button in GUI to display MPU6000 Sensor readings barry76 4 4,024 Jan-05-2019, 01:48 PM
Last Post: wuf

Forum Jump:

User Panel Messages

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