Modified simple tKinter label program to be clickable. Don't know what I did wrong

I’m barely able to do python but 6 years ago, I managed to modify a bunch of programs to create an application as a museum volunteer. Now, I am trying to use one of these both as-is, and modified to be clickable (two versions). The clickable version gets an error that I just don’t understand (which is true most of the time these days because, at 78, I have trouble remembering anything I did.

The program takes a number of parameters and outputs a label based in those parameters and can send signal to parent or kill child programs. That all works. It has been in operation for 6 years at multiple museums in an application I wrote on a Raspberry Pi. Now I need it to have a clickable way to die but send signal or kill programs just like a similar working one that does the same with pictures.

I’ve spent hours searching the web and trying to make adaptions based on similar code without success. I know it’s some dumb mistake because python is still a mystery to me. I learned Fortran in engineering school and used it for years but that was a lifetime ago.

#
# Modified by JP
# Version="4/10/19a"
Version="Rev 2.0 9/19/25 JP"
#
# run command: python3 py3_Message.py [X offset] [Y offset] [character size] [ font ] [Char Color] [BG Color] [Print string]
#
# Python3

import tkinter as tk
import sys
from pathlib import Path
import PIL
import subprocess
from subprocess import Popen

# Get variables
#
MyName=sys.argv[0]
Xoffset=sys.argv[1]
Yoffset=sys.argv[2]
Char_size=sys.argv[3]
Font_name=sys.argv[4]
fg_color=sys.argv[5]
bg_color=sys.argv[6]
Process_Id=sys.argv[7]
MyString=sys.argv[8]

print (MyName,': ',Version)


def Label_clicked(event):
    global Process_Id, MyName
    print(MyName,': ','You clicked me')
#
#  Check if Process_Id is positive or negative or zero
#
    n_Process_Id=int(Process_Id)
    if (n_Process_Id>0):
        print (MyName,": Killing children of ",n_Process_Id) 
        p1=Popen (["/usr/bin/pkill",'-P',Process_Id], stderr=subprocess.STDOUT)
    elif (n_Process_Id<0):
        n_Process_Id=(0-n_Process_Id)
        Process_Id=str(n_Process_Id)
        print (MyName,": Sending signal to ",n_Process_Id)
        p1=Popen (["/bin/kill",'-2',Process_Id], stderr=subprocess.STDOUT)
    else:
        exit(0)
 
#
master = tk.Tk()
# Remove titlebar    
master.overrideredirect(1)
# label auto-adjusts X and Y sizes to the font
label_font = (Font_name, Char_size)
master.geometry('+%s+%s' % (Xoffset,Yoffset))
Label.bind("<Button-1>", Label_clicked)
Label(master, anchor='center', text=MyString, font=label_font, bg=bg_color,
         fg=fg_color, relief='raised', bd=3).pack(fill='x', padx=0, pady=0)

master.mainloop()

This is the output message:

python3 ~/runtime/Programs.Source/py3_Message_Clickable.py ‘100’ ‘400’ “40” “Helvetica Bold” “Red” “White” 0 “OUTSIDE”
/home/admin/runtime/Programs.Source/py3_Message_Clickable.py : Rev 2.0 9/19/25 JP
Traceback (most recent call last):
File “/home/admin/runtime/Programs.Source/py3_Message_Clickable.py”, line 57, in
Label.bind(“”, Label_clicked)
^^^^^
NameError: name ‘Label’ is not defined

You haven’t defined Label. Presumably you meant tk.Label.

I changed it to:

tk.Label.bind(“”, Label_clicked)

and got

tk.Label.bind(“”, Label_clicked)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/lib/python3.11/tkinter/init.py”, line 1448, in bind
return self._bind((‘bind’, self._w), sequence, func, add)
^^^^^^^^^^
AttributeError: ‘str’ object has no attribute ‘_bind’. Did you mean: ‘find’?

You want to create a label, put it in the GUI, and then bind a mouse action to it:

# Make the label.
the_label = Label(master, anchor='center', text=MyString, font=label_font, bg=bg_color, fg=fg_color, relief='raised', bd=3)
# Put it in the GUI.
the_label.pack(fill='x', padx=0, pady=0)
# Bind a mouse action to it.
the_label.bind("<Button-1>", Label_clicked)
2 Likes

You almost got it but this time, I could fix it.

# Make the label.
the_label = Label(master, anchor='center', text=MyString, font=label_font, bg=bg_color, fg=fg_color, relief='raised', bd=3)

got an error “Label is undefined”

but this worked:

the_label = tk.Label(master, anchor='center', text=MyString, font=label_font, bg=bg_color, fg=fg_color, relief='raised', bd=3)

Thanks so much to both of you.

1 Like