【问题标题】:How to make Tkinter look good or natural on mac OS?如何使 Tkinter 在 Mac OS 上看起来不错或自然?
【发布时间】:2020-01-22 22:14:15
【问题描述】:

我使用 Tkinter、python 3.7.4 和 Mac OS Mojave 10.14.6 开发了一个简单的应用程序。 我在 Ubuntu 18.04 和最新的 Windows 10 上执行了相同的代码,应用程序看起来很原生。但是,当我在我的 Macbook 上运行它时,它看起来不像是原生的,就像其他 mac GUI 应用程序一样。

例如看这个截图:

注意小部件上的灰色背景。

下面是代码:

import datetime
import gettext
import sys
import time
import os
import tkinter
import tkinter.ttk as ttk
from tkinter.filedialog import askopenfilename
from tkinter import *
from tkinter import filedialog

# All translations provided for illustrative purposes only.
# english
_ = lambda s: s


class MainFrame(ttk.Frame):
    "Main area of user interface content."

    def __init__(self, parent):
        ttk.Frame.__init__(self, parent)
        self.parent = parent
        paddings = {'padx': 6, 'pady': 6}
        self.download_location = '/'.join(os.getcwd().split('/')[:3]) + '/Downloads'
        ttk.Label(parent, text="Youtube Url").pack(side='top', anchor='w', **paddings)
        self.entry = ttk.Entry(parent, )
        self.entry.pack(side='top', fill='x', **paddings)

        # todo delete this line
        self.entry.insert(0, 'https://www.youtube.com/watch?v=nXait2wHOQc')

        self.button = ttk.Button(parent, text="Download", command=self.do_download)
        self.button.pack(side='top', **paddings, anchor='w')

        # style = ttk.Style()
        # style.configure('TButton', foreground="red")
        # self.button.config(style='Alarm.TButton')

        self.location_button = ttk.Button(parent, text="Location", command=self.browse_button)
        self.location_button.pack(side='top', **paddings, anchor='w')

        self.statusStringVar = StringVar()
        self.statusStringVar.set('status here')
        self.status = ttk.Label(parent, textvariable=self.statusStringVar, text='status', )
        self.status.pack(side='top', anchor='w', fill='x', **paddings)

        self.locStringVar = StringVar()
        self.locStringVar.set(f"Location: {self.download_location}")
        self.locationLabel = ttk.Label(parent, textvariable=self.locStringVar, )
        self.locationLabel.pack(side='top', anchor='w', fill='x', **paddings)

        self.mp3_check_value = StringVar()
        self.mp3_checkbox = ttk.Checkbutton(parent, text='Convert to MP3')
        self.mp3_checkbox.config(variable=self.mp3_check_value, onvalue='yes', offvalue='no')
        self.mp3_check_value.set('yes')
        self.mp3_checkbox.pack(side='top', anchor='w', **paddings)

        self.progressIntVar = IntVar()
        self.progressIntVar.set(0)
        self.mpb = ttk.Progressbar(parent, orient="horizontal", length=200, mode="determinate")
        self.mpb['variable'] = self.progressIntVar
        self.mpb.pack(side='top', anchor='w', fill='x', **paddings)
        self.mpb["maximum"] = 100
        # self.mpb["value"] = 0

    def do_download(self):
        pass

    def progress_hook(self, d):
        pass

    def browse_button(self):
        filename = filedialog.askdirectory()
        print(filename)
        self.download_location = filename
        self.locStringVar.set(f"Location: {self.download_location}")


class Application(tkinter.Tk):
    "Create top-level Tkinter widget containing all other widgets."

    def __init__(self):
        tkinter.Tk.__init__(self)

        self.wm_title('Tkinter YDL')
        self.wm_geometry('640x480')

        self.mainframe = MainFrame(self)
        self.mainframe.pack(side='right', fill='y')


if __name__ == '__main__':
    APPLICATION_GUI = Application()
    APPLICATION_GUI.mainloop()

我在这里遗漏了什么吗?请帮忙。

【问题讨论】:

  • ttk 小部件是“主题”,因此您可以通过创建自定义主题使它们在 MacOS 上看起来更原生。这是关于他们的一些(有些过时但仍然适用)documentation
  • 不是所有这些小部件看起来都已经原生了吗?
  • 也许我只需要将 Frame 的背景更改为 macs 颜色。
  • @osama7901:如果您找到解决方案,请将其作为您自己问题的答案发布在这里——这是允许的——供其他人使用。

标签: python macos tkinter


【解决方案1】:

我刚刚在reddit上看到了这个主题:

https://github.com/rdbende/Sun-Valley-ttk-theme

它基于 Microsoft 视觉风格,但它看起来肯定比默认的 ttk 主题更好。

对于同一作者的另外两个主题,也可以滚动到底部。

【讨论】:

    猜你喜欢
    • 2015-12-07
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 2014-06-27
    • 2019-03-20
    • 2020-12-02
    • 2014-04-27
    相关资源
    最近更新 更多