Trouble with simple gtk python script - python

I am new to python and am trying to run a simple program I found on the web. I have installed the pyGTK All In One Download and cannot (after searching on the web for a month or so) find gtk.py anywhere.
It does not exist on my computer no matter how many times i run pygtk-all-in-one-2.24.2.win32-py2.7.msi
Here is my code
import pygtk
pygtk.require('2.0')
import gtk
class Simple:
def destroy(self, widget, data=None):
gtk.main_quit()
def __init__(self):
win = gtk.Window(gtk.WINDOW_TOPLEVEL)
win.connect("destroy", self.destroy)
win.set_title("Simple")
win.set_position(gtk.WIN_POS_CENTER)
win.show_all()
Simple()
gtk.main()
I searched this forum and many others as well. If you have this file, could you post the code so I can copy it?
If someone can help I would be very grateful. Thank You for listening.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
It seems that I have found the answer from the following link at http://www.slideshare.net/BaabtraMentoringPartner/importerror-dll-load-failed-is-not-a-valid-win32-application-python-mysql. I uninstalled the 64 bit version and installed the 32 bit version. When I run it from the command line the little window comes up! The error still shows up in Netbeans though so maybe the problem is in the Netbeans IDE?

The gtk code is in
C:\Python27\Lib\site-packages\gtk-2.0
There is no single file named gtk.py. (There is a _gtk.pyd, which is like a dll).
However, you should generally not need to access the source code for the libraries you install. Python will handle that for you when you import gtk, then you can just use those features within your script.

If you have another gtk app (like GIMP) you may have problem with enviroment. Also, I hope that the indentation is broken only here, not in your script. And tell us what is the error.

I guess that using python(64bit ) for Windows 7 was the main problem.
So I uninstalled the 64 bit version and installed the 32 bit version and it seems to work fine now.
Thanks to everyone for your help.
I would like to mark this question as answered but I don't exactly know how.

Uninstall your all-in-one and try with this other all-in-one:
https://sourceforge.net/projects/pygobjectwin32/files/
Then:
import gi
gi.require_version('Gtk', '2.0')
from gi.repository import Gtk

Related

Apps not popping up on macOS Big Sur 11.0.1

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"

Python 3 Pyperclip Installed but Module not Found

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

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

I deleted some files from my computer and now I can't run a script/program. What do I do?

I recently permanently deleted some files from computer. Now I can't run a script/program.
When I open an new terminal at the folder, I get this message...
Last login: Mon Oct 26 07:56:13 on ttys005
Vanessa-Chiangs-MacBook-Air:GTIM 0.8.2 vcchiang$ python3 GTIM.py
Traceback (most recent call last):
File "GTIM.py", line 6, in <module>
from PyQt5 import QtCore, QtGui, QtWidgets
ImportError: cannot import name 'QtCore'
Vanessa-Chiangs-MacBook-Air:GTIM 0.8.2 vcchiang$
What do I do?
You deleted something that is needed by some Python process invoked from your shell. When you invoke GTIM.py from the Terminal, some import is missing. What appears to be missing is all or parts of PyQt5.
You need to reinstall the thing that is needed by the shell startup. It looks like it is failing on some Qt Python bindings, which may either be found in core Python or via some separate library (I'm not sure, but a web search should sort that out).
Now, I did a brief search, and it appears that Python Qt5 is a separate module. Though my guess is that it is not part of the full Mac installer for Python, so you might just need to find the right Python Qt4 installer and run that.
However, if you deleted any of the stuff that came with the OS X system for Python 2 (the version that Apple ships) then you might have to restore from backup. It's going to be tricky to sort out what, exactly, you need from a Time Machine backup, but you can always just restore everything the way it was prior to when you mucked about with the system.
At this point you are going to have to do a little research, learn a little, and incrementally move forward until you know what is missing, what version, and how to replace it. We can help, but it is going to take some back and forth.
However, since we see that you are running Python3, which does not ship with OS X, you might as well just reinstall Python 3.5.0 from here. My guess might be wrong, and it might just replace the QtCore Python module that is missing. It looks like PyQt[4|5] has to be installed separately using a straightforward (but possible confusing, if you have never done it before) installation process.
https://gist.github.com/guillaumevincent/10983814
http://robscurity.blogspot.ca/2015/02/installing-pyqt-on-mac-os-x-yosemite.html
(Using Homebrew and Ruby) http://macappstore.org/pyqt5/
It is possible that this OS X machine has "Fink" or "HomeBrew" or "MacPorts" installed on it, which might make things easier, as in that last link.
Conclusion
This is the line we are concerned with:
from PyQt5 import QtCore, QtGui, QtWidgets
The QtCore Python bindings from the PyQt5 module are missing, and this Python script is failing.
Since it looks like you may have deleted parts of the PyQt5 python module bindings, you probably need to replace that, using one of the methods above. Either directly, using tools like sip, or using a helper like Fink, Homebrew, or MacPorts. It will depend on what is on this machine already (i.e., was PyQt5 installed with Homebrew already? I can't tell you this) and how good you are at figuring out the links I provided.
Your first step should be seeing if any of these commands are found when invoking them at a Terminal:
fink
brew
port
If any of these return anything other than "command not found" (or similar), then you should be able to figure out how to use that command to re-install the PyQt5 Python bindings.

Error: no module named gtk.glade

I researched and tried this for two days now and I cannot get gtk to work on Windows 7 with Python 3.4! Whenever I launch my .py file on Python 3.4 with import gtk, I get No module named gtk! I installed pygobject but it did not help. Even gtk3-demo command works in the windows cmd prompt.
I finally got gtk to import (I think) by copying the GTK directory right to C:\Python34\Lib. But now I have a problem with gtk.glade.
Where is this? Where do I copy it from and to where?
You are possibly using an outdated tutorial, see the current Python GTK 3 tutorial for a more up-to-date reference.
In particular, the way to import GTK has changed in GTK3 to:
from gi.repository import Gtk
And instead of libglade, you would use the newer Gtk.Builder class like so:
ui = Gtk.Builder()
ui.add_from_file("my_glade_file.glade")
(you still develop the UI using Glade, it is only how you access it from your program that has changed).

Categories