简体   繁体   English

如何在 Mac OS 上使用 Tkinter 获取黑色文件对话框?

[英]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).我正在尝试实现黑色文件对话框(Mac OS 深色模式)。 I'm using Tkinter filedialog module ( import tkinter.filedialog ).我正在使用 Tkinter 文件对话框模块( import tkinter.filedialog )。

  • Mojave (10.14.4) dark mode Mojave (10.14.4) 暗模式
  • Python 3.6.8蟒蛇 3.6.8
  • Tcl/Tk 8.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.当我从任何其他应用程序打开文件对话框时,它们具有黑色背景,但是当我从tkinter.filedailog打开它时,它们具有默认的白色背景。

Here is the image of file dialog opened by Tkinter :这是Tkinter打开的文件对话框的图像:

#图片1

Black file dialog supported by Mojave dark mode when opened from elsewhere:从别处打开时 Mojave 暗模式支持的黑色文件对话框:

#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.如果有任何方法可以使用Tkinter获得黑色文件对话框,请帮助我我真的很想要黑色对话框。

sample.py样本.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.可能会出现无法让您在 Tkinter GUI 上获得适当暗模式支持的问题。 All of these were are on my Mac it could differ in every system.所有这些都在我的 Mac 上,每个系统可能会有所不同。

STEP 1: First thing you need to force dark mode to those apps which do not support dark mode officially.步骤 1:首先,您需要对那些正式不支持暗模式的应用程序强制暗模式。

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.如果您想恢复为默认设置,只需使用以下命令删除NSRequiresAquaSystemAppearance设置。

defaults delete -g NSRequiresAquaSystemAppearance

STEP 2: How to FIX the black Tkinter window?第 2 步:如何修复黑色的 Tkinter 窗口?

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.如果您使用 Anaconda,那么您只需要执行 1st Step 即可在所有应用程序上获得暗模式,然后从命令行将 Tcl/Tk 更新到 8.6.9。 ( 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. Anaconda 已将 Tcl/Tk 更新至 8.6.10 并添加了一个支持 macOS 不同外观模式(深色、浅色)的新命令,这意味着更改任何模式都会更改窗口和小部件的背景颜色,但它是一个有点毛病。 And also we have to pass foreground = 'black to see the text of Button而且我们还必须通过foreground = 'black来查看Button的文本在此处输入图片说明 and for some other widgets as well.以及其他一些小部件。

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

Python.org 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.解决第一个问题后,您将在 Tkinter 上获得暗模式,但如果您有 Tcl/Tk 8.6.8,则在 Tkinter 窗口上会出现黑屏。

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.此问题已在 Tcl/Tk 8.6.9 中修复,但由于 python.org 尚未对其进行更新,并且还提供了他们自己的 Tcl/Tk 8.6.8 私有副本。 They do not look for or use any third-party or system copies of Tcl/Tk( More Details ).他们不寻找或使用 Tcl/Tk 的任何第三方或系统副本(更多详细信息)。 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.我测试了内置 Tcl/Tk 8.6.9 的Python 3.7.2rc1 ,它在 Mojave 暗模式下运行良好,但由于在 Tk 8.6.9.1 中发现了一些回归,他们恢复了已发布的 python.org 3.7.2 macOS 安装程序到 Tcl/Tk 8.6.8。

Sample Image示例图像

I'm not a GUI developer, but I'm pretty sure OSX won't let you change the colour of entities such as a dialog window...我不是 GUI 开发人员,但我很确定 OSX 不会让您更改对话框窗口等实体的颜色...

If it helps, I ran your code and got the following:如果有帮助,我运行您的代码并得到以下内容:

成功

I am using OSX Dark Mode though...不过我正在使用 OSX 暗模式...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用 tkinter 的情况下在 python 中打开保存文件对话框 - how to open the save file dialog box in python without using tkinter 使用tkinter创建一个从对话框中提取文件的ftp应用程序 - Using tkinter to create an ftp app that pulls file from dialog box 如何制作一个简单的 tkinter 输入框,使用 os 创建一个用户输入作为名称的文件? - How can I make a simple tkinter input box that creates a file with user input as name using os? 使用Python Tkinter在对话框中选择后如何检索文件名 - How to retrieve file name after choosing in dialog box using Python Tkinter 如何在tkinter中使用文件对话框来获取文件名? - How to use a file dialog in tkinter to get a filename? 使用 tkinter 按钮实现时,如何正确获取 tkinter 文件对话框的结果? - How to properly get the results of a tkinter file dialog when implemented using tkinter button? 在Mac OS X上使用Tkinter和Matplotlib重复对话窗口 - Repeated dialog window with Tkinter and Matplotlib on Mac OS X 如何将复选框与“打开文件”对话框链接并显示使用python tkinter在GUI中打开的文件的详细信息? - How do I link checkbox with file open dialog box & display the details of the file opened in GUI using python tkinter? 使用 tkinter Mac OS 时按钮闪烁 - Buttons Flickering while using tkinter Mac OS Mac OS 上的 Python TKinter 不显示消息框特定图标 - Python TKinter on Mac OS does not show message box specific icons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM