Python - Tkinter not support MacOS Mojave Dark Mode
Asked Answered
C

2

11

I have a problem with the support of Dark Mode on MacOS in python Tkinter. I used python 3.6 with ActiveTlc 8.5 and the Dark Mode works fine, the window titlebar was dark, it's fine for me... but there were some problems with the <MouseWheel> support, then I upgraded python to 3.7.1 and the version of tlc is updated to 8.6.

But now the Dark Mode didn't work, and it's strange, why this is happening?

This is an example code:

from tkinter import *

if __name__ == '__main__':
    root = Tk()
    hero_text = Label(root, fg='white', bg='black', text='HERO TEXT')
    hero_text.grid(row=0, sticky=N+W)
    print(root.tk.exprstring('$tcl_library'))
    print(root.tk.exprstring('$tk_library'))
    root.mainloop()
Chaffee answered 22/10, 2018 at 12:46 Comment(4)
You're lucky it's just the dark mode. On my system TK applications just show empty, black windows. I would recommend switching to a more modern toolkit like GTK+ 3 or Qt 4.Recital
@Recital This issue has been fixed in 8.6.9 according to tk developers core.tcl.tk/tk/tktview?name=821dbe47e1Pulverable
This post can help if someone on Mac wants Mojave Dark Mode on Tkinter applications #55484007Shaw
You can fix this issue by installing the newest python.Prothalamion
C
1

I also faced this kind of problem, I think you should try this

from tkinter import *

if __name__ == '__main__':
       root = Tk()
       root.configure(bg="black")
       hero_text = Label(root, fg='white', bg='black', text='HERO TEXT')
       hero_text.grid(row=0, sticky=N+W)
       print(root.tk.exprstring('$tcl_library'))
       print(root.tk.exprstring('$tk_library'))
       root.mainloop()
Cleanse answered 3/5, 2022 at 9:45 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Wheeling
A
0

Using the base tkinter module, there is no way to automate dark mode without another module like subprocess. I recommend you add a setting for dark mode, but if you want it to still be automated here is the code to do it:

from tkinter import *
import subprocess

def check_appearance():
   cmd = 'defaults read -g AppleInterfaceStyle'
   p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE, shell=True)
   if bool(p.communicate()[0]):
      return "black"
   else:
      return "white"


win = Tk()
win.configure(bg=check_appearamce())

win.mainloop()
Anemic answered 23/12, 2022 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.