Started messing with Tkinter today, but when I tried to run my first program it crashed. It appears the reason is that I don't have Tkinter. This is a standard installation of Python 3.3 on Windows 7, why is it not there? How can I get it?
This answer might be irrelevant with more information, but, for now: are you capitalizing "Tkinter" when using the import command? In Python 3.x, it's imported as lower-case ("import tkinter"), but in 2.x code it's imported with an initial capital ("import Tkinter").
Maybe you disabled it during Python installation? It is Tcl/Tk item in install wizard and it can be disabled. Try reinstall Python and do not turn it off.
Both of the answers above are perfectly good ideas but if neither of these work make sure you are using this code:
from tkinter import *
Rather than this code:
import tkinter
Related
It is always risky to upgrade your operation system. It is likely you will encounter some compatibility issue. I took the risk to upgrade my macOS from Catalina to the newest Big Sur. After that, the display in the new OS looks pretty, but all my PyQt5 apps could not be launched in this new OS. The GUI window does not pop up as usual, and there is no error message showing in the terminal. I spent the whole day trying to figure out what makes this problem. I found the solution but in a weird way which I feel confused.
It turns out that the apps comes back to normal after I add the following three lines in the main script.
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
It seems to me the new OS has some compatibility issue with Qt5Agg back-end. But the strange thing is that this solution also works for one of the Pyqt5 app, where I don't use matplotlib at all.
The Python version I used is 3.8.4, and the PyQt5 version I have is 5.15.1.
I hope somebody could explain to me what happen under the hood that makes this solution work. Also I hope this temporary solution can help somebody with the same problem.
A reply to the PyQt mailing list pointed out that setting this env var works:
QT_MAC_WANTS_LAYER=1
Found via Is there any solution regarding to PyQt library doesn't work in Mac OS Big Sur? and https://forums.macrumors.com/threads/pyqt5-and-big-sur.2260773/?post=29243620#post-29243620
I can confirm that matplotlib.use('TkAgg') followed by matplotlib.use('Qt5Agg') makes things work for me, too. I whittled it down to this as also working:
# from matplotlib.backends import _tkagg
import _tkinter
import matplotlib.pyplot as plt
plt.figure()
So it's something about the compiled _tkinter module. Maybe the inputhook?
As #Eric said, just add the following on the very start of your code, before the PySide2 import:
import os
os.environ["QT_MAC_WANTS_LAYER"] = "1"
Then import PyQt5/PySide2.
I followed the solution here and downgraded to PyQt 5.13. This solved my issue and allowed my compiled apps to run on Big Sur.
pip install PyQt5==5.13
for me the suggested solution brought a crashes on a breackpoints in pycharm ... the only thing helped :
https://forums.macrumors.com/threads/pyqt5-and-big-sur.2260773/
all worked as a magic ...
hope QT will fix it soon
I am using macOS Big Sur Version 11.2.2
As suggested by Eric, enter the following line in the terminal before launching your application:
export QT_MAC_WANTS_LAYER=1
This fixed the issue for me!
Adding this to my python program worked for me
import os
os.environ["QT_MAC_WANTS_LAYER"] = "1"
I'm trying to copy some text to clipboad in my python program, so I've installed pyperclip via pip command via Windows command line interface, it says everything is successfully installed, not a problem. However, when I import the thing into my project, I get
from tkinter import *
from tkinter import ttk
import pyperclip
import binascii
#my code...
...
ModuleNotFoundError: No module named 'pyperclip'
So I was like *** that, maybe it's broken, I found a little library that does exactly the same, called "clipboard". Exactly the same installation procedure - from the command line. Same successful installation. Same ModuleNotFoundError. So clearly something wrong on my side, but I have no idea what it is, no idea where to look and what to do. I just want to be able to copy some text to clipboard. Multiplatform. That's it.
It's Python 3.8, Windows 10
pyperclip-1.8.0
pip-20.2.2
If I need to show you some logs or tell you something about my installation, please tell me what exactly to do and where to find information you may need. I'm good with instructions, but I don't have much (any) experience rummaging through logs and python installation folders.
Similar posts have slightly different problems, I didn't find any clear solution or hints that I could understand. No reply seemed like a solution to me.
Anyway, I would really appreciate any help from the community. Being unable to simply copy something to clipboard is killing me, especially since it's the only thing I can't implement. And it's just one function for one button to copy one short piece of text. It's like being stuck at 99% loading, when everything is done, and you just can't write the final line of code, haha.
Pycharm uses a virtualenv for pycharm projects. Navigate through:
Pycharm >> File >> Settings(or press Ctrl+Alt+S on win) >> Project:Name >> Project Interpreter >> Click on the plus(+) symbol above the scroll bar and type in pyperclip and press install package.
And this will install pyperclip for your Pycharm IDE. Since pycharm uses a virtual env for its projects you can change it in the project interpreter section as well, and then the pyperclip you installed from the cmd using pip, will be available to be used on that global version of python(now used by your Pycharm too).
If you still have any errors or doubts, do let me know :D
Cheers
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
I'm trying my hands on Turtle but I can't get the module installed.
I have searched a lot and people seems to imply that it is included in the Python
standard library with Tkinter but this doesn't seem to be the case for me.
when I do:
import Tkinter
everything seems ok. But when I try
t1 = Turtle()
I get the error
NameError: name 'Turtle' is not defined
As per the tutorial I'm suppose to import like this:
from turtlegraphics import Turtle
But no matter what I've tried I cant find how I can get the library installed.
You need to import it from the turtle module.
from turtle import Turtle
t1 = Turtle()
t1.forward(100)
Try to check within the library files whether there is a file called turtle in it . I had the same problem and i found the library file and opened it. So check it and see if it will fix your problem. Most probably turtle library file will be in Lib folder.
Nothing here worked for me. This is what I did: I checked what configuration I had (in the upper right hand corner). I changed it so it was the python version that ran through the project and not just plain python. Then you also have to go to script path (also a setting in the configuation) and set it to the .py that you are working on :)
hey all of you guys just check importing that module ,you dont get any errors its already in standard libraray
I have downloaded python 2.5. I would like to know if Tkinter is included with python or is it a separate download?
Yes, it is included.
http://wiki.python.org/moin/TkInter
If you are using linux just open your terminal and type python and in the python interpreter type from Tkinter import* if it doesn't show any error messages you are good to go. You can try this to check every package of python like Pygame just replace Tkinter by Pygame