gpt4 book ai didi

python - 如何使 Tkinter 在 mac OS 上看起来好看或自然?

转载 作者:行者123 更新时间:2023-12-05 08:12:42 31 4
gpt4 key购买 nike

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

例如看这个截图: enter image description here

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

代码如下:

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 框架。这就是背景颜色不同的原因。您应该将 self 设置为标签的父级。

ttk.Label(self, text="Youtube Url").pack(side='top', anchor='w', **paddings)

关于python - 如何使 Tkinter 在 mac OS 上看起来好看或自然?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58052323/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com