Python Forum
[Tkinter] HOW TO: Set [Label].config() by variable name reference?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] HOW TO: Set [Label].config() by variable name reference?
#1
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).
Reply
#2
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'}

test_config(**config)
Output:
('Liberation Mono', 20, 'bold') red3 pink ('Liberation Mono', 20, 'bold') red3 pink
Reply
#3
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" ))
cBox[0].grid(row=0,column=0)

looper()
root.mainloop()
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/config.py", line 29, in <module>
    looper()
  File "/media/mint/DISTROS/linux-mint/m18p3-64b/py-dev/idle3/py3-work/demorefs/config.py", 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
Reply
#4
cBox[0].config(**style(i))
Use [] not ()

cBox[0].config(**style[i])
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'}]

test_config(**style[0])
test_config(**style[1])
test_config(**style[2])
Output:
('Liberation Mono', 20, 'bold') red3 pink ('Liberation Mono', 20, 'normal') black dark goldenrod ('Liberation Mono', 20, 'bold') dark goldenrod light yellow
Reply
#5
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!) ...
  cBox[0].config(**style(i))
  #   SHOULD BE:
  cBox[0].config(**style[i])
  #                     ^ ^
That resolves the error and the demo runs fine

Blessings!
Art in Carlisle, PA USA
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  update text variable on label with keypress knoxvilles_joker 3 4,835 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  Printing Variable into the label ardvci 5 3,033 Mar-19-2020, 09:35 PM
Last Post: joe_momma
  [Tkinter] Unable to save filepath to config.ini file using filedialog.askopenfilename JackMack118 10 4,892 Dec-29-2019, 08:12 PM
Last Post: JackMack118
  [Tkinter] Trying to display a variable in a label Jordansonatina 2 17,671 Oct-31-2019, 06:28 PM
Last Post: Jordansonatina
  [Tkinter] can't update label to include variable foxtreat 2 3,605 Dec-01-2018, 07:16 AM
Last Post: jfong
  printing option menu variable in label in Tkinter SmokerX 1 6,591 Jan-18-2018, 07:36 PM
Last Post: SmokerX

Forum Jump:

User Panel Messages

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