`tkinter._test()` buttons are invisible when using pipenv

北城余情 提交于 2020-05-15 06:19:44

问题


I'm learning Tkinter right now and trying to work through my first issue, but finding Google hasn't been helpful.

I'm running this code on Mac OS X 10.15.1 (Catalina)

I'm using Python 3.7 and my code looks like so (lots of boilerplate per PEP8 standards):

"""
Experiments in tkinter
"""


import tkinter as tk


def main():
    """
    Main entrypoint
    """

    tk._test()


if __name__ == "__main__":
    main()

This outputs the following warning to the console:

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.

It also displays a window with completely blank buttons:

From my understanding these buttons should say "Click Me" and "Quit".

Also when I click on the top button it flashes black for a second like so:

After which the button is wider than before:

I assume the Tk version error may be related to the output behavior. However I can't seem to find a way to update Tk. What should I do here?

Update

After seeing @ShayneLoyd's answer (suggesting I needed an updated version of Python with an updated version of Tk) I began looking around some more and found this post on the Apple Discussion Boards which suggests you can use homebrew to install a version of Python which link's homebrew's own installed version of Tk. I tried this and it failed, so I Google'd the issue and found this StackOverflow post which seemed to suggest I can install ActiveTcl and it will work.

I installed ActiveTcl from ActiveState and went back to my project. At first, I ran it like so:

$> cd ~/Source/experiments/python/tkinter
$> ./test.py

This actually worked! I could read the buttons and it behaved like it should. Then I realized I was not using pipenv, so I did a quick test:

$> pipenv run ./test.py

Now I was back to the failure state. I updated my script to display the Tcl and Tk version and sure enough when I used pipenv it was loading Tcl/Tk 8.5.9 but when I didn't use pipenv it was loading 8.6.9

So how do I fix pipenv now?


回答1:


After a few hours messing with it I figured it out!

The Pipfile contained a reference to Python version 3.7

Therefore pipenv install was searching my machine for any 3.7 version of Python installed. The version it found was Homebrew's, which has Tk 8.5.9 statically linked.

By updating the Pipfile to require Python 3.8 my machine started pulling my system Python, which uses the dynamically linked Tk, which I had successfully updated to 8.6.9




回答2:


For anyone who's having the same issue and needs to stick to his Python version, you can easily bypass the "not showing text on buttons" problem by changing MacOs theme. System Preferences>General>Light Mode.

You should have in mind that you may encounter other problems.



来源:https://stackoverflow.com/questions/58752385/tkinter-test-buttons-are-invisible-when-using-pipenv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!