Pergunta

I am having a problem with tkinter.ttk on mac. I am using macports and python3.1. When I try to use tkinter.ttk I get very old looking gui elements.

eg: I get this
enter image description here
Instead of this:
enter image description here

The code I used is:

from tkinter import *
from tkinter import ttk
root = Tk()
button = ttk.Button(root, text="Hello World").grid()
root.mainloop()

I would be happy to provide any information from my computer needed to answer this question. As I am a novice programer please tell me where to find said information.

I have a Macbook 5,2 with Snow Leopard installed. Any help would be appreciated.
Thanks, Marlen

Question Update:
I installed tk @8.5.9_0+quartz as schlenk suggested only to get this error:

TclMacOSXNotifierAddRunLoopMode: Tcl not built with CoreFoundation support Abort trap

I fixed this error with the patch from https://trac.macports.org/ticket/22954. I followed the instructions to the letter(they are):

$ cd /opt/local/var/macports/sources/rsync.macports.org/release/ports/lang/tcl
$ sudo patch < ~/Downloads/tcl.2.patch 
$ sudo port install tcl 

This created a new error which is:

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.
Foi útil?

Solução

The problem might be macports. There are three versions of Tk you could use as the basis for your ttk. The screenshot looks a lot like the older X11 Tk, not the aqua based Tk. 1. Tk via X11. 2. Tk compiled with Carbon 'windowingsystem -aqua' 3. Tk compiled with Cocoa

So you should try to either build a Tk variant 'quartz' via macports or you should get some prebuilt version (e.g. ActiveStates) that has the right version already built.

So try:

sudo port build tk @8.5.9+quartz

Have a look at tutorials here for some more guidance: http://www.tkdocs.com/tutorial/install.html#installmac

Outras dicas

try

style = ttk.Style()
print(style.theme_names())
style.theme_use('default') # change 'default' to something better

I haven't played with ttk, however I have a decent amount of experience with tkinter. I belive you have to fill out the style keyword argument.

I think it would look something like this.

from tkinter import *
from tkinter import ttk
root = Tk()
button = ttk.Button(root, text="Hello World", style="somestyle").grid()
root.mainloop()

Link to some relevant documentation: http://docs.python.org/release/3.1.3/library/tkinter.ttk.html

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top