Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAC: Command Window Button Background Colors Do Not Show #51

Open
tbpassin opened this issue Dec 17, 2023 · 3 comments
Open

MAC: Command Window Button Background Colors Do Not Show #51

tbpassin opened this issue Dec 17, 2023 · 3 comments
Labels
bug Something isn't working Mac-specific

Comments

@tbpassin
Copy link
Owner

tbpassin commented Dec 17, 2023

A user with a Macintosh reports that command buttons do not change their background colors when moused over. The color is used for several purposes, including indicating that extended help messages are available.

Without a Mac to test with, this will be hard to diagnose. With one it might be possible depending on Tk and its quirks on the Mac.

See https://stackoverflow.com/questions/72056706/tkinter-button-background-color-is-not-working-in-mac-os

https://www.reddit.com/r/learnpython/comments/npdwsp/tkinter_button_color_wont_change/

tkmacosx is pip-installable. The project is at

https://pypi.org/project/tkmacosx/

It looks like it might do the job for very little code change. Of course, A Mac is needed for testing.

@HaveF
Copy link

HaveF commented Dec 17, 2023

tkmacosx works.

import tkinter as tk
from tkmacosx import Button
def on_enter(e):
    button.config(bg='blue')
def on_leave(e):
    button.config(bg='red')
root = tk.Tk()
button = Button(root, text="Hover Me")
button.pack(pady=20)
button.bind("<Enter>", on_enter)
button.bind("<Leave>", on_leave)
root.mainloop()

@tbpassin
Copy link
Owner Author

Excellent! The GF4 code involved in the file cmdwin.py. This can be run as a standalone window.

If you are going to try working with the code, please read https://github.com/tbpassin/gf4-project/blob/devel/DEVELOPERS_READ_THIS.md first.

My first thought is to change the import import tkinter to import tkmacosx as tkinter. However, the code also has:

try:
    import tkFont
except ImportError:
    import tkinter.font as tkFont

try:
    import ttk
except ImportError:
    from tkinter import ttk

(some of this is left over from when GF4 used to support both Python 2.7 and 3.x. I can never remember which branch is for which, so I've just left both alternatives in. I could change these conditional imports and maybe I should finally do it).

I don't know if these statements would work with tkmaxosx or not. If not, we would have to make more elaborate code changes (probably not much more complex, though).

What would be especially good is if tkmacosx would also work on Windows and Linux. I will try it in a venv.

The code that dynamically changes the button background color is in the function definitions def on_enter(event): and def click(event):.

@tbpassin
Copy link
Owner Author

tbpassin commented Dec 18, 2023

Here is a minimal program that opens a Tk window and displays a button that changes color when hovered. It uses the same techniques as GF4's cmd.py button window.

from tkinter import Tk, Button

BG_INITIAL = 'white'
BUTTON_HOVER = 'yellow'
BUTTON_BG = 'cyan'
BG_KEY = 'bg'

def on_enter(event):
    w = event.widget
    try:
        w.old_bg = w.cget('bg')
        w[BG_KEY] = BUTTON_HOVER
    except Exception as e:
        print(e)

def on_leave(event):
    w = event.widget
    _bg = w.old_bg
    w[BG_KEY] = BUTTON_BG

# create a tkinter window
main = Tk()
# Open window having dimension 200x100
main.geometry('200x100')
# Create a Button
b = Button(main, text='Hover Over Me',  bg = BG_INITIAL)
b.pack()
b.bind('<Enter>',  on_enter)
b.bind('<Leave>',  on_leave)

main.mainloop()

This code works as expected on Windows and Linux. Although on Linux, the background color is the system default, not the one specified in the code. Changing BG_KEY to BG_KEY = 'activebackground' does make the hover color as specified. BUT this causes the Windows hover color not to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Mac-specific
Projects
None yet
Development

No branches or pull requests

2 participants