Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

python - How to get a black file dialog box using Tkinter on Mac OS?

I'm trying to achieve a black file dialog box (Mac OS dark mode). I'm using Tkinter filedialog module (import tkinter.filedialog).

  • Mojave (10.14.4) dark mode
  • Python 3.6.8
  • Tcl/Tk 8.6.8

When I open file dialog from any other app they have black background but when I open it from tkinter.filedailog they have default white background.

Here is the image of file dialog opened by Tkinter:

#Image1

Black file dialog supported by Mojave dark mode when opened from elsewhere:

#Image2


If there is any way to get the black file dialog box with Tkinter, Please help me I really want the black dialog box.

sample.py

import tkinter.filedialog as _FD

_Master = _FD.Tk()
_Master.withdraw()

from kivy.core.window import Window as _kivy_window

class Open(_FD.Open):
    def __init__(self, multiple=False, **options): 
        if multiple: options["multiple"]=1
        super(Open, self).__init__(**options)

    def show(self, **options):
        s = super().show(**options)
        _kivy_window.raise_window()
        return s

if __name__ == "__main__":

    from kivy.app import App
    from kivy.uix.button import Button
    _kivy_window.size = (250, 250)

    class TestApp(App):
        def open(self, *a):
            s = Open(multiple=True)
            s = s.show()
            if s: print(s)

        def build(self):
            return Button(text='Hello World', on_release=self.open)

    TestApp().run()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Possible issues that won't let you have proper Dark Mode support on Tkinter GUI. All of these were are on my Mac it could differ in every system.

STEP 1: First thing you need to force dark mode to those apps which do not support dark mode officially.

By default, the dark mode doesn't apply to every application like some apps that are third parties and from untrusted developers. We can still achieve Dark Mode to those apps, but not every app will work properly maybe that's why it is not an option in the setting.

If you’re not confident in using the command line you probably should not do this.

  1. Enable the Dark Mode and then run this command in terminal:

     defaults write -g NSRequiresAquaSystemAppearance -bool No
    

    Note: "Yes" means disable to all windows and "No" means enable to all.

  2. After running the command log out and log back in to notice the changes.

If you want to revert back to default settings, simply delete the NSRequiresAquaSystemAppearance setting with the following command.

defaults delete -g NSRequiresAquaSystemAppearance

STEP 2: How to FIX the black Tkinter window?

For Anaconda

If you use Anaconda then you just need to perform 1st Step to get the Dark Mode on all apps then update Tcl/Tk to 8.6.9 from the command line. (More Details)

conda install -c conda-forge tk 
conda install -c conda-forge/label/gcc7 tk 
conda install -c conda-forge/label/broken tk 
conda install -c conda-forge/label/cf201901 tk

Results

image image

UPDATE:

Anaconda has updated Tcl/Tk to 8.6.10 and also added one new command which supports different appearance modes of macOS (dark, light), which means changing any mode will change the background color of the window and widgets as well but it is a bit glitchy. And also we have to pass foreground = 'black to see the text of Button enter image description here and for some other widgets as well.

conda install -c conda-forge/label/cf202003 tk
image image

Python.org

After solving the first issue you will get Dark Mode on Tkinter but a black screen on the Tkinter window if you have Tcl/Tk 8.6.8.

Sample Image

enter image description here

This problem has fixed in Tcl/Tk 8.6.9 but as python.org has not updated it and also supplies their own private copies of Tcl/Tk 8.6.8. They do not look for or use any third-party or system copies of Tcl/Tk(More Details). So it'll be a waste of time if you thinking to install it from third parties.

I tested Python 3.7.2rc1 which is built-in Tcl/Tk 8.6.9 and it works well with Mojave Dark Mode but due to some regressions found in Tk 8.6.9.1, they reverted the released python.org 3.7.2 macOS installers back to Tcl/Tk 8.6.8.

Sample Image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...