Python Forum
Problem with tkinter widget list
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with tkinter widget list
#1
This is part of the Enigma project that I am working on.
I'm adding a new post here, because this is a question having to do specifically with a GUI issue.
The program I am working on is PatchBoard.py to follow.
The images used (and expected in a sub directory named image)
can be downloaded here: https://github.com/Larz60p/Enigma/tree/master/src/image
or just clone the unfinished project with: git clone https://github.com/Larz60p/Enigma
PatchBoard.py:
import tkinter as tk
import tkinter.font as tkFont
import EnigmaPaths
import json


class PatchBoard:
    def __init__(self, parent):
        """
        Initialization
        :param parent: Containing tkinter widget
        """
        self.parent = parent
        self.patch_jacks = [ ]
        self.num_colors = 13
        self.patch_color_used = [0] * self.num_colors
        print(f'patch_color_used: {self.patch_color_used}')
        self.epath = EnigmaPaths.EnigmaPaths()

        with self.epath.enigma_info.open() as f:
            self.rotor_info = json.load(f)

        self.alpha = self.rotor_info['unencoded']

        with self.epath.color_info.open() as f:
            self.colors = json.load(f)

        self.parent.update()
        self.parent_width, self.parent_height = parent.winfo_width(), parent.winfo_height()
        print(f'width: {self.parent_width}, height: {self.parent_height}')

        # print(self.colors)
        self.bgcolor = self.colors['.w3-theme-d5']['background-color']

        self.patchbaord = tk.Frame(self.parent, relief=tk.RAISED, bg=self.bgcolor)
        self.patchbaord.grid(row=0, column=0, sticky='nsew')
        self.create_jacks()

    def create_jacks(self):
        self.patch_jacks = [ ]
        plug = self.epath.imagepath / 'plug1.png'
        self.customFont = tkFont.Font(family="Helvetica", size=10)
        self.img = tk.PhotoImage(file=plug)
        self.topframe = tk.Frame(self.patchbaord, relief=tk.RAISED, padx=2, pady=2, bg=self.bgcolor)
        self.middleframe = tk.Frame(self.patchbaord, relief=tk.RAISED, padx=2, pady=2, bg=self.bgcolor)
        self.bottomframe = tk.Frame(self.patchbaord, relief=tk.RAISED, padx=2, pady=2, bg=self.bgcolor)
        self.topframe.grid(row=0, column=0)
        self.middleframe.grid(row=1, column=0)
        self.bottomframe.grid(row=2, column=0)
        for n, i in enumerate('QWERTZUIO'):
            row = [ ]
            row.append(i)
            row.append('base') # slot for color
            row.append(tk.Label(self.topframe, bg='black', fg='white', text=i,
                                font=self.customFont).grid(row=0, column=n))
            row.append(tk.Button(self.topframe, bd=0, image=self.img,
                                 command=lambda jack=i: self.light_me_up(jack)).grid(row=1, column=n))
            self.patch_jacks.append(row)

        for n, i in enumerate('ASDFGHJK '):
            row = [ ]
            row.append(i)
            row.append('base') # slot for color
            if i == ' ':
                row.append(tk.Label(self.middleframe, bg='black', fg='white',
                                    text=i).grid(row=0, rowspan=2, column=n))
                row.append(None)
            else:
                row.append(tk.Label(self.middleframe, bg='black', fg='white', text=i,
                                    font=self.customFont).grid(row=0, column=n))
                row.append(tk.Button(self.middleframe, bd=0, image=self.img,
                                     command=lambda jack=i: self.light_me_up(jack)).grid(row=1, column=n))
            self.patch_jacks.append(row)

        for n, i in enumerate('PYXCVBNML'):
            row = [ ]
            row.append(i)
            row.append('base') # slot for color
            row.append(tk.Label(self.bottomframe, bg='black', fg='white', text=i,
                                font=self.customFont).grid(row=0, column=n))
            row.append(tk.Button(self.bottomframe, bd=0, image=self.img,
                                 command=lambda jack=i: self.light_me_up(jack)).grid(row=1, column=n))
            self.patch_jacks.append(row)
        for row in self.patch_jacks:
            print(f'row: {row}')

    def light_me_up(self, p_index):
        print(f'{p_index} Pushed')
        # Let there be light!
        subidx = [p_index in x for x in self.patch_jacks]
        n_index = [i for i, x in enumerate(subidx) if x is True][0]
        print(f'n_index: {n_index}')
        print(f'self.patch_jacks[n_index]: {self.patch_jacks[n_index]}')
        color_idx = self.patch_color_used.index(0)
        if self.patch_jacks[n_index][1] == 'base':
            try:
                print(f'color_idx: {color_idx}')
                t_image = self.epath.imagepath / f'color{color_idx+1}.png'
                newimg = tk.PhotoImage(file=t_image)
                self.patch_jacks[n_index][3].configure(image=newimg)
                self.patch_jacks[n_index][3].image = newimg
                self.patch_color_used[color_idx] = 1
            except:
                pass
        else:
            # toggle unlit
            self.patch_jacks[n_index][3].configure(image=self.img)
            self.patch_jacks[n_index][3].image = self.img
            self.patch_color_used[color_idx] = 0



if __name__ == '__main__':
    root = tk.Tk()
    root.title('Test window for patchboard')
    # root.geometry('600x400+10+10')
    app = PatchBoard(root)
    root.mainloop()
when I run this, I am printing out the values that I am storing in my button array,
but none are appending, each entry should have the format:
['letter value', color_index, tk.Label, tk.Button]
but what I get (partial list)is:
Output:
row: ['Q', 'base', None, None] row: ['W', 'base', None, None] row: ['E', 'base', None, None] row: ['R', 'base', None, None] row: ['T', 'base', None, None] row: ['Z', 'base', None, None] row: ['U', 'base', None, None] row: ['I', 'base', None, None] row: ['O', 'base', None, None] row: ['A', 'base', None, None]
I'm not sure why the widget entries are showing up as None.
So, why?
Reply
#2
The method grid of a widget, returns None.
Instead of appending the object direct to the list, you should use following pattern:

for col, text in enumerate('PYXCVBNML'):
    label = tk.Label(self.bottomframe, bg='black', fg='white', text=text, font=self.customFont)
    button = tk.Button(self.bottomframe, bd=0, image=self.img, command=lambda jack=text: self.light_me_up(jack))
    label.grid(row=0, column=col)
    button.grid(row=1, column=col)
    self.patch_jacks.append([text, 'base', label, button])
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
I understood with your first sentence. Thanks!
I don't usually append grid to the definition, but did for whatever reason this time.
It was driving me nuts, because I have used arrays of this sort in the past without issue.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 842 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,830 Jun-26-2022, 06:26 PM
Last Post: menator01
  Tkinter Exit Code based on Entry Widget Nu2Python 6 2,982 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  tkinter text widget word wrap position chrisdb 6 7,538 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  Python3 tkinter radiobutton problem Nick_tkinter 14 5,988 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter
  tkinter python button position problem Nick_tkinter 3 3,557 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  Tkinter - How can I extend a label widget? TurboC 2 2,773 Oct-13-2020, 12:15 PM
Last Post: zazas321
  [Tkinter] ClockIn/Out tkinter problem Maryan 2 2,196 Oct-12-2020, 03:42 AM
Last Post: joe_momma
  tkinter| listbox.insert problem Maryan 3 3,496 Sep-29-2020, 05:34 PM
Last Post: Yoriz
  Tkinter problem DPaul 6 4,109 May-28-2020, 03:40 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

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