Tkinter buttons are completely white Mac - python

I'm creating a simple UI, my problem is that buttons are always white also if I change all their color properties...I'm new in Tkinter maybe I'm missing something.
This is the code, you can just copy and run:
import tkinter
tk = tkinter.Tk()
def browseFiles():
print("Browsing...")
browseButton = tkinter.Button(
tk, text="Browse files", command=browseFiles, fg='#03045e', bg='#caf0f8', background='#ef233c', activeforeground='blue').pack()
tk.mainloop()
This is a picture of result:
MacOS version : 11.5.2 withPython 3.10.0rc2, tcl-tk 8.6.11_1 and when I run the code this is the warning :
DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
Maybe I should downgrade to a python stable version ?

Solved :
pip3 install tkmacosx
This will install some extensions that will fix the problem.
Just one thing be sure to switch interpreter, maybe you're not using the right one.
In my case it worked when I used this one .
Check where tkmacosx has been installed, you should use the same interpreter version I guess.

Related

Tkinter full screen not working in Python 3.10 for Mac OS

I just upgraded Python from 3.8 to 3.10 and the Tkinter full screen attribute no longer works as it used to.
I'm using Mac OS Big Sur 11.6.6 (Macbook M1). I had Python 3.8 installed on this Macbook and it has been working fine until I upgraded Python today.
Here is the code and the root window does not show in full screen for Python 3.10:
if __name__ == '__main__':
root = tkinter.Tk()
root.attributes('-fullscreen', True)
root.mainloop()
Any help would be much appreciated because I'm stuck. I have also seen other problems with my UI in Python 3.10 so if I'm not able to solve these problems I may revert back to Python 3.8 :(
UPDATE 1 I've noticed that my Macbook makes a bleep sound when I run with the full screen attribute but it does not if I comment out that row.
I've also noticed that it makes the same bleep sound when I type on the keyboard e.g. in an Entry widget. It did not use to do that in Python 3.8, so may be related.
UPDATE 2 I am able to do a somewhat ugly workaround by using a delay and setting the full screen attribute after 250 ms with a root.after(...) function. Then it works. It does not look very nice since empty white backgrounds flash by before the actual program opens.
You need to use root.wm_attributes() instead.
Example
import tkinter as tk
root = tk.Tk()
root.wm_attributes('-fullscreen','true')
root.mainloop()
Yet another way to do this is by calling tcl as follows
root.tk.call("::tk::unsupported::MacWindowStyle", "style", root._w, "plain", "none")
use only either of the methods.

Tkinter install not working after pip install tk

I need Tkinter to use in one of my projects as a button etc.
i pip installed Tkinter with the code below as i followed a tutorial online and it said to
pip install tk
is this not correct as when I import Tkinter import Tkinter. it says module not found?
I have tried to restart my project and have tried every possible pip install combination ever.
Please help explain Tia.
If you are using Python 3, Tkinter is built in. But you have to use a lowercase letter. Don't use "Tkinter", use "tkinter" instead.
Also, don't use "import tkinter", use "from tkinter import *", to import everything.
Example:
from tkinter import *
def command():
print("Hello, world!")
root = Tk()
root.title("tkWindow")
btn = Button(root, text="Click Me!", command=command)
btn.pack()
root.mainloop()
Also, remember that when you import everything from Tkinter (using from tkinter import *), you don't use tk.Tk() or tk.Button(), you just have to use Tk() and Button().
This should work for you. Good luck, and happy coding!
For me, it depended on what version of python I was using. When I was trying to do turtle graphics for the first time, my Tkinter module wasn't importing. After from tkinter import Tk, it worked. When we moved to python 3.8.5, it clearly had tkinter built into python 3 already, so try pip installing (just in case), then try from tkinter import Tk again. If it isnt working, check to make sure that you're on the latest version of python!

Tkinter keeps crashing on Tk() on Mac

I've encountered a problem while attempting to create a Tkinter window using root = tk.Tk() . Every time I get to that point, the program crashes, and "Python quit unexpectedly" message is displayed.
I get no tracebacks at all, so I assume that is the ActiveTcl bug. However, I have the new distribution from ActiveTcl Website installed, which is supposed to take care of the problem (obviously, it doesn't).
Interestingly enough, it only crashes when it is executed in Python 2.7. It works fine in Python 3.6. However, I need to use 2.7.
My MacOS version is 10.12.5.
Any ideas / suggestions about fixing the issue are welcome.
P.S. I have read a good dozen of similar posts before posting this, and not any of the proposed solutions worked for me. Please consider this before marking this post as a duplicate.
I don't know what is meant by "new distribution" for ActiveTcl is but if you're using 8.6, it needs to be downgraded to 8.5.
Also, if you run IDLE which uses Tkinter, do you see any messages warning of "instability"? If you see that then, it means you need to downgrade Tcl to 8.5.
Are you running Python 3 through Anaconda? Tkinter was acting all sorts of funky on me and then I uninstalled Anaconda and now it works fine.
Interestingly enough, I am running a PyCharm Professional / Anaconda combo on a Windows 10 VM on my Mac, and I have issues with Tkinter on it as well. I have absolutely no issues however on my Linux Mint box.
To reiterate, I was able to remove the issue by completely removing Anaconda. (How to uninstall Anaconda completely from macOS)
Tkinter needs to be imported like this in order to work with both python 2 and 3:
try:
import tkinter
except ImportError: # python 2
import Tkinter as tkinter

TTK theme not working, just old-fashioned tkinter

On my computer with Win8.1 when I use following code
from tkinter import *
from tkinter.ttk import *
Tk().mainloop()
I see only the old looking tkinter. Any idea how to fix it?
I tried to use different theme with no result.
For some reason following code:
from tkinter import *
from tkinter.ttk import *
Style().theme_use("alt")
Tk().mainloop()
results with two identical tkinter old-fashioned windows which seems not to be correct.
PS: It seems to be issue related with Win8.1, see here:
Python 2.7 - ttk module seemingly not working in Windows 8.1
But I found no information how to fix it. Any news would be welcome. :)
EDIT1:
After trying following snippet:
from tkinter import *
from tkinter.ttk import *
from functools import partial
root = Tk()
style = Style(root)
def change(name, style):
style.theme_use(name)
for s in style.theme_names():
lb = Button(root, text=s, command=partial(change, s, style))
lb.pack()
I could say the themes are changing not only for color but completely. What is still puzzling me is I cannot see the nice fancy graphic which I expect to see.
I tried to change my windows settings for performance or to visual aspect but it is not the reason why it is not working.
If you have Win 8.1 do you have tkk working as expected?
EDIT2:
This is my look using example from
http://www.tkdocs.com/tutorial/firstexample.html
(Win7 and Win8.1)
The difference can be easily seen on ubuntu between the themes.
So, maybe it is a problem with win8.1. Considered using the ttkthemes module? The ttkthemes module is another set of more "Stylish" styles.
To install:
sudo apt-get install python3-tk
sudo -H pip3 install ttkthemes
To know more, go to:
https://github.com/RedFantom/ttkthemes/wiki/Usage
I have answered another similar question,you can refer to it through this link:
How to make pyinstaller import the ttk theme?
Lemme know in the comments if this also, doesn't work.

How to Install and Use TkDnD with Python Tkinter on OSX?

I've spent some time to search for workable solution to do Drag and Drop behavior with Python Tkinter on OSX platform, and the most possible solution found is TkDnD library.
http://sourceforge.net/projects/tkdnd/files/
However I cannot find any manual or guide about the installation and basically no sample on OSX. Can anyone share their experience with me?
Furthermore, is it not a good choice to use Tkinter as a GUI solution? My users are all OSX platform and Python is preinstalled on all these machines. Any good suggestion to find a native GUI support without additional installation? PyQT seems to be another choice, but not sure if it requires the additional installation on Client machine.
I got this working on both Windows (10) and OSX (10.11) by downloading:
A) Tk extensions tkdnd2.8 from https://sourceforge.net/projects/tkdnd/
B) Python wrapper TkinterDnD2 from https://sourceforge.net/projects/tkinterdnd/
On OSX:
1) Copy the tkdnd2.8 directory to /Library/Tcl
2) Copy the TkinterDnD2 directory to /Library/Frameworks/Python.framework/Versions/.../lib/python/site-packages
(Use the sudo command for copying files on OSX due to permissions.)
On Windows:
1) Copy the tkdnd2.8 directory to ...\Python\tcl
2) Copy the TkinterDnD2 directory to ...\Python\Lib\site-packages
And here's a simple test case based on python drag and drop explorer files to tkinter entry widget. The TkinterDnD2 download comes with much more robust examples.
import sys
if sys.version_info[0] == 2:
from Tkinter import *
else:
from tkinter import *
from TkinterDnD2 import *
def drop(event):
entry_sv.set(event.data)
root = TkinterDnD.Tk()
entry_sv = StringVar()
entry_sv.set('Drop Here...')
entry = Entry(root, textvar=entry_sv, width=80)
entry.pack(fill=X, padx=10, pady=10)
entry.drop_target_register(DND_FILES)
entry.dnd_bind('<<Drop>>', drop)
root.mainloop()
Update: the above procedure works for Python 2 or 3.
This is an update from 2017, with a bit more detail, since Mac decided to make it impossible to write files to /System/Library. The following solution is also cross-platform, since I am currently writing an app for both Windows/Mac that uses TkDnD.
The following solution works with pyInstaller, and also with Mac OS 10.12 and Windows 7.
First, we need to get a path to where tkDnD is. By default, I place tkdnd2.8 folder next to main.py.
import sys, os
if getattr(sys, 'frozen', False):
# If the application is run as a bundle, the pyInstaller bootloader
# extends the sys module by a flag frozen=True and sets the app
# path into variable _MEIPASS'.
application_path = sys._MEIPASS
else:
application_path = os.path.dirname(os.path.abspath(__file__))
TK_DND_PATH = os.path.join(application_path,'tkdnd2.8')
Note that Ellis's solution works Tcl directly, modifying the path. Make sure that SOMEWHERE, you have something along this gist:
import tkinter as tk
root = tk.Tk()
root.eval('lappend auto_path {' + TK_DND_PATH + '}')
After this, whenever you happen to actually import tkDnD, it will find it. I used DnD.py. Without the 'lappend auto_path' command, my program could never find tkDnD.
https://mail.python.org/pipermail/tkinter-discuss/2005-July/000476.html
2021 update
it annoyed me enough,so after the author did not responded I've forked the repo and built wheel for the latest compiled release(2.9.2) and upload it to pypi
you can now just do pip install tkinterdnd2 and it should work.
import is tkinterdnd2 and not Tkinterdnd2. see demos
I spent a few days to figured out how to install TkDnD lib, although it sounds like an easy question but did confused me a little while.
Two ways to install TkDnD on OSX:
A. Copy to /System/Library/Tcl/8.x:
OSX preinstalled Python already, and this is the path where Tcl library is installed. TkDnd lib will be loaded automatically while using Tk/Tcl lib.
B. Set os.environ['TKDND_LIBRARY'] to the location of TkDnd2.x.dylib:
Sample code:
if sys.platform == 'win32':
if getattr(sys, 'frozen', False):
os.environ['TKDND_LIBRARY'] = os.path.join(os.path.dirname(sys.executable), 'tkdnd2.7')
This is an update from 2020 for MacOS Catalina users.
Firstly, the tkdnd library project has been moved to github. The latest version is 2.9.3, and if you do not want to compile the library yourself the latest release is 2.9.2.
Secondly, placing the tkdnd lib on /Library/Tcl doesn't work anymore (you will get "error: tkdnd library not found"). With Catalina, python looks for the tkdnd lib in its own folder, that is /Library/Frameworks/Python.framework/Versions/.../lib. Placing the tkdnd2.9.3 folder in this path works just fine.
By the way, placing the TkinterDnD2 Python wrapper in /Library/Frameworks/Python.framework/Versions/.../lib/python/site-packages still works on Catalina.
Just to clarify, I only tested it for Python 3 (3.8.5), I do not know if for Python 2 the solution is the same but I suppose so.

Categories