[Python] Can't change color of buttons in tkinter on Macbook

Discussão em 'Python' iniciado por Stack, Janeiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    Working on my first (school) project using tkinter, python 3 and im having to teach myself this stuff along the way. I want to change the color of my buttons but it is not working. For context, I'm using a MacBook and Jupyter notebook. No error is being given and nothing is wrong with the code, as far as I'm aware.

    When I tried installing tkmacosx using pip and it gives an error. Here is my code below:

    import tkinter as tk

    from tkmacosx import Button

    # Deals with main screen details

    root = tk.Tk()

    root.title("main screen")
    root.configure(bg = '#1B5004')
    root.geometry("1500x1000")

    # End of main screen settings

    # Title

    title = tk.Label(text= "California's Native Wildlife", bg = '#1B5004', font = ("Times", 30))
    title.place(x = 550, y = 20)

    # Buttons redirecting to the two other screens

    vegetation = tk.Button(root, text = "Vegetation", highlightbackground = '#14d659', highlightcolor = '#14d659', font = ("Arial", 25))
    vegetation.pack(pady = 20)

    root.mainloop()


    This is the error it gives me when I added the tkmacosx button import:

    AttributeError Traceback (most recent call last)
    Cell In[5], line 3
    1 import tkinter as tk
    ----> 3 from tkmacosx import Button
    5 # Deals with main screen details
    7 root = tk.Tk()

    File /opt/anaconda3/lib/python3.12/site-packages/tkmacosx/__init__.py:27
    15 """
    16 This module provides some modified widgets of Tkinter which works better on macos
    17 and some more useful functions and classes as well. For example Button of tkmacosx which
    (...)
    22 https://github.com/Saadmairaj/tkmacosx#tkmacosx
    23 """
    25 __version__ = '0.1.6'
    ---> 27 from tkmacosx.basewidget import check_appearance, get_shade, delta
    28 from tkmacosx.variables import ColorVar, DictVar, SaveVar
    29 from tkmacosx.widget import Button, CircleButton, SFrame, Marquee, Radiobutton

    File /opt/anaconda3/lib/python3.12/site-packages/tkmacosx/basewidget.py:1221
    1215 raise _tk.TclError(
    1216 'bad anchor position "{}": must be n, ne, e, se, s, sw, w, nw, or center'.format(anchor))
    1218 return self.coords(item, x, y)
    -> 1221 class ButtonBase(_Canvas, _button_functions):
    1222 """Internal class used for tkinter macos Buttton"""
    1224 def __init__(self, _type=None, master=None, cnf={}, **kw):

    File /opt/anaconda3/lib/python3.12/site-packages/tkmacosx/basewidget.py:1309, in ButtonBase()
    1306 _return.extend(self._features)
    1307 return sorted(list(set(_return)))
    -> 1309 @tkv._colorvar_patch_destroy
    1310 def destroy(self):
    1311 """Destroy this and all descendants widgets. This will
    1312 end the application of this Tcl interpreter."""
    1313 main_win = self.winfo_toplevel()

    AttributeError: module 'tkmacosx.variables' has no attribute '_colorvar_patch_destroy'


    Any help and advice is appreciated

    Continue reading...

Compartilhe esta Página