Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] HOW TO: Set [Label].config() by variable name reference?
Esteemed Forum Participants and Lurkers:
Python 3.5.2 (Linux Mint 18.3 Mate 64-bit) Tk 8.6.5 IDLE 3.5.2

I am NOT a software developer, but I have been a hobyist/hacker since I first had unlimited access to a Bendix G15 (vacuum tube!) computer a "few" years ago.

I am trying to create an app with tkinter that involves a primitve level of 'animation' in a tkinter widget main window. So far I can create a grid array (a List) of single-character tk.Labels and operate on them to change foreground and background colors, bold/normal style, text cursor, and text (by variable reference), all using the {label}.config() widget option, as shown in the following example:

cBox[n].config(font=("Liberation Mono", 20, "bold"), fg="red3", bg="pink")
I have about 10 different 'styles' of Labels. I have found docs on how to change the font by variable reference, but is there any way to define a full ".config()" and reference the various configs by a defined name?

I have been searching for hours, and have come up with some very interesting web sites, but no help on "config by name". Thank you for any and all comments, suggestions, and assistance in this effort.

Blessings in abundance, all the best, & ENJOY!
Art in Carlisle, PA USA

Confucius c. 500 BC: Love of knowledge (Python tkinter) without the love of learning (interminable searching) falls into presumption (and abject frustration).
I think this is what you mean
def test_config(font, fg, bg):
    print(font, fg, bg)

test_config(font=("Liberation Mono", 20, "bold"), fg="red3", bg="pink")

config = {'font': ('Liberation Mono', 20, 'bold'),
          'fg': 'red3',
          'bg': 'pink'}

('Liberation Mono', 20, 'bold') red3 pink ('Liberation Mono', 20, 'bold') red3 pink
Thanks for the quick response Yoriz ...

I see that you are creating a sort of 'config style' to pass to a function with test_config(**config). Here is a better example of what I am trying to do ...

import tkinter as tk

root = tk.Tk()   # Create Window
cBox = []        # List of Labels by ID
iTxt = "A"       # Text for Labels
i = 0            # Global index counter

# List of various styles to be implemented by name (small sample) per your example
style = [
  {'font': ('Liberation Mono', 20, 'bold'), 'fg': 'red3', 'bg': 'pink'},
  {'font': ('Liberation Mono', 20, 'normal'), 'fg': 'black', 'bg': 'dark goldenrod'},
  {'font': ('Liberation Mono', 20, 'bold'), 'fg': 'dark goldenrod', 'bg': 'light yellow'}]

# Function to continuously change the style of the Label text
def looper():
  global i                     # index that is retained between calls
  print(i)                     # Debug - show looper is running
  # ********    CRITICAL LINE    ********
  cBox[0].config(**style(i))   # CHANGE THE CONFIG OF THE LABEL BY NAME "style[n]"
  # *************************************
  i += 1                       # Increment the style selection
  if i > 2: i = 0              # Limit range 0-2
  root.after(1000, looper)     # Schedule looper with delay of 1 sec

# Define and load the Label in the window
cBox.append(tk.Label(root, text=iTxt[0],
    borderwidth=1, font=("Liberation Mono", 20, "normal"),
    fg="black" ))

The "critical line" in function looper is trying to load different "style"s in the Label on each pass of looper().

I get an error when I run this:
Traceback (most recent call last):
  File "/media/mint/DISTROS/linux-mint/m18p3-64b/py-dev/idle3/py3-work/demorefs/", line 29, in <module>
  File "/media/mint/DISTROS/linux-mint/m18p3-64b/py-dev/idle3/py3-work/demorefs/", line 18, in looper
    cBox[0].config(**style(i))   # CHANGE THE CONFIG OF THE LABEL BY NAME "style[n]"
TypeError: 'list' object is not callable
Thanks for your help ... I hope this is an understandable illustration of exactly what I am trying to do.

Blessings in abundance,
Art in Carlisle, PA USA
Use [] not ()

def test_config(font, fg, bg):
    print(font, fg, bg)

style = [
  {'font': ('Liberation Mono', 20, 'bold'), 'fg': 'red3', 'bg': 'pink'},
  {'font': ('Liberation Mono', 20, 'normal'), 'fg': 'black',
   'bg': 'dark goldenrod'},
  {'font': ('Liberation Mono', 20, 'bold'), 'fg': 'dark goldenrod',
   'bg': 'light yellow'}]

('Liberation Mono', 20, 'bold') red3 pink ('Liberation Mono', 20, 'normal') black dark goldenrod ('Liberation Mono', 20, 'bold') dark goldenrod light yellow
Thanks Yoriz !!!

Of COURSE - "style" is a LIST, so [] is required !!! My oversight. My brain is foggy.

Just tried it and it works GREAT !!!

Thank you for resolving this for me ... I have been beating my head against a wall for hours, and never would have gotten the solution but for your input. This really simplifies my code. I can either load styles from an array, or I can give each style a descriptive name! WOOHOO!

I'll edit my previous post and show the correction, and mark the thread as solved.

Blessings in abundance, all the best, & ENJOY!
Art in Carlisle, PA USA

Thank you Yoriz !!!

In line 19 of the code in the 3rd post in this thread (I can't edit it!) ...
  #   SHOULD BE:
  #                     ^ ^
That resolves the error and the demo runs fine

Art in Carlisle, PA USA

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] Looking For Good Reference Material rebeltaz 2 36 3 hours ago
Last Post: rebeltaz
  [Tkinter] Please help, my Label does not show up on my gui ? robertinoc 2 189 Jun-12-2019, 05:58 PM
Last Post: robertinoc
  [PyQt] How to reference QTreeView QHeaderView setResizeMode Denni 1 166 Jun-11-2019, 10:20 PM
Last Post: Alfalfa
  [Tkinter] Having trouble updating the text in a label microphone_head 3 277 May-02-2019, 08:44 AM
Last Post: microphone_head
  [PyQt] How to access a label in an other def ? Suppenbrot 1 201 Apr-27-2019, 10:32 AM
Last Post: danielsvens
  [Tkinter] So what do I need to do change the color this label? jpezz 10 488 Apr-03-2019, 09:49 PM
Last Post: jpezz
  tkinter label error nonzzo 1 284 Mar-31-2019, 01:29 PM
Last Post: Larz60+
  Display and update the label text which display the serial value jenkins43 5 528 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  [Tkinter] can't update label to include variable foxtreat 2 504 Dec-01-2018, 07:16 AM
Last Post: jfong
  [Tkinter] Updating Label After Button Press malonn 7 1,026 Aug-23-2018, 10:52 PM
Last Post: malonn

Forum Jump:

Users browsing this thread: 1 Guest(s)