如何在 Mac OS 上使用 Tkinter 获取黑色文件对话框? [英] How to get a black file dialog box using Tkinter on Mac OS?

查看:59
本文介绍了如何在 Mac OS 上使用 Tkinter 获取黑色文件对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现黑色文件对话框(Mac OS 深色模式).我正在使用 Tkinter 文件对话框模块(import tkinter.filedialog).

  • Mojave (10.14.4) 暗模式
  • Python 3.6.8
  • Tcl/Tk 8.6.8

当我从任何其他应用程序打开文件对话框时,它们具有黑色背景,但是当我从 tkinter.filedailog 打开它时,它们具有默认的白色背景.

这是Tkinter打开的文件对话框的图片:

从别处打开时 Mojave 暗模式支持的黑色文件对话框:

<小时>

如果有什么办法可以用Tkinter获得黑色文件对话框,请帮帮我,我真的很想要黑色对话框.

sample.py

将 tkinter.filedialog 导入为 _FD_Master = _FD.Tk()_Master.withdraw()from kivy.core.window import Window as _kivy_window类打开(_FD.Open):def __init__(self, multiple=False, **options):如果有多个:options["multiple"]=1super(Open, self).__init__(**options)定义显示(自我,**选项):s = super().show(**选项)_kivy_window.raise_window()返回如果 __name__ == "__main__":从 kivy.app 导入应用程序从 kivy.uix.button 导入按钮_kivy_window.size = (250, 250)类 TestApp(App):def open(self, *a):s = 打开(多个=真)s = s.show()如果 s:打印定义构建(自我):返回按钮(文本=你好世界",on_release=self.open)测试应用程序().运行()

解决方案

可能导致您无法在 Tkinter GUI 上获得适当暗模式支持的问题.所有这些都在我的 Mac 上,每个系统可能会有所不同.

第 1 步:首先,您需要对那些不正式支持暗模式的应用强制使用暗模式.

默认情况下,暗模式并不适用于所有应用程序,例如某些第三方应用程序和不受信任的开发人员的应用程序.我们仍然可以为这些应用程序实现暗模式,但并非每个应用程序都能正常工作,这可能是设置中没有选项的原因.

如果您对使用命令行没有信心,您可能不应该这样做.

  1. 启用暗模式,然后在终端中运行此命令:

     默认写 -g NSRequiresAquaSystemAppearance -bool 否

    注意:是"意味着禁用所有窗口和否"表示对所有人启用.

  2. 运行命令后注销并重新登录以注意更改.

如果您想恢复为默认设置,只需使用以下命令删除 NSRequiresAquaSystemAppearance 设置即可.

defaults delete -g NSRequiresAquaSystemAppearance


第 2 步:如何修复黑色的 Tkinter 窗口?

对于蟒蛇

如果您使用 Anaconda,那么您只需要执行第一步以在所有应用程序上获取暗模式,然后从命令行将 Tcl/Tk 更新到 8.6.9.( 以及其他一些小部件.

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


Python.org

解决第一个问题后,您将在 Tkinter 上获得暗模式,但如果您有 Tcl/Tk 8.6.8,则在 Tkinter 窗口上会出现黑屏.

示例图片

此问题已在 Tcl/Tk 8.6.9 中修复,但由于 python.org 尚未对其进行更新,并且还提供了他们自己的 Tcl/Tk 8.6.8 私有副本.他们不寻找或使用 Tcl/Tk 的任何第三方或系统副本(更多详细信息).因此,如果您想从第三方安装它,那将是浪费时间.

我测试了内置 Tcl 的 Python 3.7.2rc1/Tk 8.6.9 并且它在 Mojave 暗模式下运行良好,但由于在 Tk 8.6.9.1 中发现了一些回归,他们将发布的 python.org 3.7.2 macOS 安装程序恢复到 Tcl/Tk 8.6.8.

示例图片

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:

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


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()

解决方案

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

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 and for some other widgets as well.

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


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

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

这篇关于如何在 Mac OS 上使用 Tkinter 获取黑色文件对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆