pythonmacostkinterlabelwindow

Python -- tkinter window label not displaying correctly with "relief" option enabled


I'm trying to put a label (including a few words of text + 1 image) in a window. As a learning process, I also used the "relief" option within the label to put a nice border around the label.

But this "relief" doesn't seem to be working as intended:

Quick info about my software versions:

=> OS: macOS Catalina 10.15.7
=> IDE: PyCharm CE
=> Python version: 3.12.5
=> tkinter Tcl version: 8.6 (from importing tkinter and using: print(tkinter.TclVersion))

The following is my code. There's so little in there.

# ===================================================================
# imports and version checking
from tkinter import *
import tkinter
import sys
print(sys.version)
print(tkinter.TclVersion)

# create a window
window = Tk()

# prepping image to add to label; just some random picture I had laying around
photo = PhotoImage(file='Avatar - bkgnd.png').subsample(4,4)

# create a label, args: container (which is window in this case), and the text option
label = Label(window,
              text="Hello",
              font=("TkDefaultFont",40,"bold","underline"),
              fg='#00ff00',
              bg='black',
              relief=RAISED,      # This option is causing problems; SUNKEN, RAISED, do not work; GROOVE, RIDGE work.
              bd=10,
              padx=20, pady=20,
              image=photo,
              compound=CENTER)

# add label to window
label.pack()

# display the window
window.mainloop()
# ===================================================================

In my research I came across potential macOS issues with tkinter, including several questions & answers here on Stack Overflow, but found no solutions.

Would you happen to have any suggestions as to how I might be able to fix my problem?

It must be so simple, but I'm just not seeing it, and it's driving me crazy.


Solution

  • Thanks to @cratermoon on Discord, who helped me find a work-around that worked. The work-around is to put a frame around the label.

    Sounded convoluted to me initially, but it wasn't difficult to implement, even for me as a beginner in various respects. Basically it's like: window wrapping around frame -> frame wrapping around label

    So in my case, I successfully implemented it as follows:

    # ===================================================================
    # create a window
    window = Tk()
    
    # prepping image to add to label; just some random picture I had laying around
    photo = PhotoImage(file='Avatar - bkgnd.png').subsample(4,4)
    
    # CROSS-PLATFORM COMPATIBILITY:
    # -----------------------------
    #  tcl for macOS Catalina is faulty -- relief doesn't work in label; must do "relief"
    #  in frame. So like: window wrapping around frame, frame wrapping around label.
    #  ("tkmacosx" is good for buttons only; it has no Label.)
    frame = Frame(window, relief=SUNKEN, bd=10)   # Doing "relief" and "bd" options in frame,
    frame.pack()                                  #  instead of in label.
    
    # Create label. However, the "master" argument becomes "frame", as we're
    #  wrapping the frame around the label.
    label = Label(frame,
                  text="Hello",
                  font=("TkDefaultFont",40,"bold","underline"),
                  fg='#00ff00',
                  bg='black',
                  #relief=SUNKEN,   # Doing "relief" option in frame instead of here in label
                  #bd=10,           # Doing "bd" option in frame instead of here in label
                  padx=20, pady=20,
                  image=photo,
                  compound=CENTER)
    
    label.pack()    # Packing label into frame
    
    # display the window
    window.mainloop()
    # ===================================================================