我在mac上遇到tkinter.ttk问题。我正在使用macports和python3.1。当我尝试使用tkinter.ttk时,我会看到非常古老的gui元素。
例如:我明白了
而不是这个:
我使用的代码是:
from tkinter import *
from tkinter import ttk
root = Tk()
button = ttk.Button(root, text="Hello World").grid()
root.mainloop()
我很乐意提供回答此问题所需的计算机信息。由于我是一名新手,请告诉我在哪里可以找到所述信息。
我有安装了Snow Leopard的Macbook 5,2。任何帮助将不胜感激 谢谢,Marlen
问题更新:
我安装 tk @ 8.5.9_0 + quartz 作为 schlenk 建议只是为了得到此错误:
TclMacOSXNotifierAddRunLoopMode: Tcl not built with CoreFoundation support Abort trap
我使用https://trac.macports.org/ticket/22954中的补丁修复了此错误。我按照说明书(他们是):
$ cd /opt/local/var/macports/sources/rsync.macports.org/release/ports/lang/tcl
$ sudo patch < ~/Downloads/tcl.2.patch
$ sudo port install tcl
这会产生一个新错误:
Traceback (most recent call last):
File "hello.py", line 5, in <module>
root = Tk()
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/__init__.py", line 1632, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: Can't find a usable tk.tcl in the following directories:
/opt/local/lib/tcl8.5/tk8.5 /opt/local/lib/tcl8.5/tk8.5/Resources/Scripts /opt/local/lib/tk8.5 /opt/local/lib/tk8.5/Resources/Scripts /opt/local/Library/Frameworks/Python.framework/Versions/3.1/Resources/Python.app/Contents/lib/tk8.5 /opt/local/Library/Frameworks/Python.framework/Versions/3.1/Resources/Python.app/Contents/lib/tk8.5/Resources/Scripts /opt/local/Library/Frameworks/Python.framework/Versions/3.1/Resources/Python.app/lib/tk8.5 /opt/local/Library/Frameworks/Python.framework/Versions/3.1/Resources/Python.app/Contents/library
/opt/local/lib/tk8.5/tk.tcl: version conflict for package "Tk": have 8.5.7, need exactly 8.5.9
version conflict for package "Tk": have 8.5.7, need exactly 8.5.9
while executing
"package require -exact Tk 8.5.9"
(file "/opt/local/lib/tk8.5/tk.tcl" line 20)
invoked from within
"source /opt/local/lib/tk8.5/tk.tcl"
("uplevel" body line 1)
invoked from within
"uplevel #0 [list source $file]"
This probably means that tk wasn't installed properly.
答案 0 :(得分:3)
问题可能是macports。有三个版本的Tk可以用作你的ttk的基础。屏幕截图看起来很像旧的X11 Tk,而不是基于aqua的Tk。 1.通过X11转发。 2.使用Carbon'windowingsystem -aqua'编译的Tk 3.用Cocoa编译的Tk
所以你应该尝试通过macports构建一个Tk变体'quartz',或者你应该得到一些已经构建了正确版本的预建版本(例如ActiveStates)。
所以试试:
sudo port build tk @8.5.9+quartz
在这里查看教程以获得更多指导: http://www.tkdocs.com/tutorial/install.html#installmac
答案 1 :(得分:2)
试
style = ttk.Style()
print(style.theme_names())
style.theme_use('default') # change 'default' to something better
答案 2 :(得分:-1)
我没有玩过ttk,但是我对tkinter有很多经验。我相信你必须填写样式关键字参数。
我认为它看起来像这样。
from tkinter import *
from tkinter import ttk
root = Tk()
button = ttk.Button(root, text="Hello World", style="somestyle").grid()
root.mainloop()
链接到一些相关文档: http://docs.python.org/release/3.1.3/library/tkinter.ttk.html