Python.exe keeps running after closing Gtk.main() when both Gtk and win32ui modules are imported - windows

I run python 2.7.13 on windows 7.
I am creating a window with Gtk (from pygobject 3.18.2).I also use win32ui (from pywin32 221).When I import both modules my program runs just fine untill I close it. The window is closing fine, but the python process keeps running and the cmd window that is used to run the script does not return to de cmd prompt.I have to kill python to get back to the promptHere is a simple test script. This does not close proper on my system. If I comment out
#import win32ui
it will close proper
from gi.repository import Gtk
import win32ui
class Window(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.connect("destroy", Gtk.main_quit)
self.show_all()
Window()
Gtk.main()
Looks like a conflict between Gtk and win32ui.I need win32gui and win32ui for extracting icons from pe files.What can I do?

On previous versions of PyWin32:
https://sourceforge.net/p/pywin32/bugs/609/
https://sourceforge.net/p/pywin32/bugs/636/
import atexit, os
def taskkill_this():
# kill this process
current_pid = os.getpid()
os.system("taskkill /pid %s /f" % current_pid)
atexit.register(taskkill_this)
Appears to be one provided workaround.
Upon further inspection, it appears to be related to pywin32 and Gtk UI event processes clashing. I would recommend using one or the other rather than both if possible or use a taskkill hack like the one above.
I've also noticed that this has happened on Windows 7 workstations in all cases I have seen.

Related

Problem with destroy in tkinter under Anaconda Spyder

In the following program, I can close the window with its close button but neither with the 'Esc' binding, nor the button callback. I am sent to the IPython console but the window stays here with a spinning wheel ! Works fine in the Terminal or with Anaconda IDLE. I am on Mac, High Sierra, Anaconda and Spyder last versions (IPython 7.1.1, Spyder 3.3.2). I suspect a problem with Spyder.
from tkinter import *
class Myapp(object):
def __init__(self):
self.root = Tk()
self.root.geometry('150x100+1+1')
self.root.title('Root')
self.root.bind('<Escape>', lambda e: self.root.destroy())
self.button = Button(self.root, text='End Program', command=self.end)
self.button.place(x=10, y=45)
self.L = [1,2,3] # result of an omitted computation
def end(self):
self.root.destroy()
app = Myapp()
app.root.mainloop()
print(app.L)
Any hint ? Thanks.
(Spyder maintainer here) You have two options to make your code work in Spyder:
Go to
Tools > Preferences > IPython console > Graphics
and deactivate the option called Activate support. Then after running your code, it'll block the console, but you won't have any other problems with it.
If you selected Tk as the backend in
Tools > Preferences > IPython console > Graphics
then you need to remove app.root.mainloop() from your code because using our Preferences creates a Tk event loop so your code doesn't block the console, and that makes app.root.mainloop() unnecessary.
Destroy doesn't work in spyder for me either, but does work fine from the system's Terminal. Carlos Cordoba's advice doesn't help.
I'm on a Mac OS Mojave, with most recent versions of Anaconda Python and Spyder available for Mojave.
I would post this as a comment, but don't have the reputation to do that.

Python modules not working in IDLE

Working on Automate The Boring Stuff project and having trouble with pyperclip and IDLE. pyperclip is successfully downloaded and works fine in Terminal but when I import pyperclip in IDLE, I get the below error.
UPDATE - This is happening with other modules - anything I've installed in the terminal aren't importing into IDLE.
import pyperclip
Traceback (most recent call last):
File "", line 1, in
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
I figure I have the module saved in the wrong folder somehow. Below are the sys.path outputs from my Terminal and IDLE.
In terminal:
'/anaconda/bin',
'//anaconda/lib/python36.zip',
'//anaconda/lib/python3.6',
'//anaconda/lib/python3.6/lib-dynload',
'//anaconda/lib/python3.6/site-packages',
'//anaconda/lib/python3.6/site-packages/Sphinx-1.5.1-py3.6.egg',
'//anaconda/lib/python3.6/site-packages/aeosa',
'//anaconda/lib/python3.6/site-packages/IPython/extensions',
'/Users/andrewricardo/.ipython']
In IDLE:
sys.path
['', '/Users/andrewricardo/Documents', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages']
There are multiple paths here - how do I effectively move the pyperclip module to the correct place?
OR - Is there something else going on here?
Each python installation and uses its own .../lib/site-packages for 3rd party modules, and you have two. If you ran the non-anaconda python in the terminal, the same that is running IDLE, it would have the same problem. Indeed, that python is the source of the ImportErrors.
Option 1: Separately install all packages you want to use with the non-anaconda python (and IDLE) in its own site-package directory. In IDLE Shell, get the path to its executable
>>> import sys; sys.executable
Save are remember the resulting 'python-path'. Then in the terminal, run
python-path -m pip install package-name
for each package.
Option 2 (uses a little known feature of site-package directories): in
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
add a file name, for instance, anaconda.pth, containing one line.
//anaconda/lib/python3.6/site-packages
(I have no idea why the double / or if it is really needed; I just copied it from your question.) This makes the anaconda python site-packages an extension of the framework python site-packages. Since both are for 3.6, one copy should work for both.
Had this same issue and googled
'why is importing pyperclip in my python file giving an "modulenotfounderror"but working fine in the idle'
and this page came up. then i also checked this [installed Pyperclip, trouble importing to IDLE.
I am new to python, had the same issue using the same book after pip installing pyperclip here's what I did to solve this issue.
I copied the pyperclip-1.7.0-py3.7.egg-info and pyperclip folders from
C:\Users\USER\AppData\Local\Programs\Python\Python37\Lib\site-packages into the C:\Users\USER\Anaconda3\Lib\site-packages folders and C:\Users\USER\AppData\Local\Programs\Python\Python37 folders and it worked. Hope this helps. If it doesn't. Try this code in your text editor because that's what I did using Atom and how i was able to solve it.
import sys
print(sys.executable)
Then copy the pyperclip-1.7.0-py3.7.egg-info and pyperclip folders into the path that the above code gives you.

"Module use of python34.dll conflicts with this version of Python"

My knowledge of Python is still pretty basic, and I am only now trying to wrap my head around how to use / call libraries from within Maya. (Because I need to create a basic UI from QT Designer and have it be opened in Maya after converting it to a .py file)
After I learned to properly convert a .ui to a .py, I now get this error in Maya
"Module use of python34.dll conflicts with this version of Python"
I tried following what was said here and here, but even then - after setting these environment variables...
PYTHONHOME = C:\Program Files\Autodesk\Maya2016\bin\maya.exe
PYTHONPATH = C:\Python34
... I a still unable to run a basic .py file. In fact - as long as the PYTHONHOME variable is in effect, Python from within Maya no longer does anything.
This code below is the resulting python file that I got from converting the .ui file that I saved out of QT Designer.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.4.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
I've come across some different online notes that advised the use of PySide, and after trying to use:
from PySide.QtCore import *
from PySide.QtGui import *
I still couldn't test if this works, because apparently QtWidgets isn't a module?
I'm pretty lost. The bigger picture is that I just want to be able to run a ui created from QT Designer from Maya's script editor. I have no idea where to begin with learning about libraries. Would anyone here be kind enough to give some advice?
Additional info:
I am using Maya 2016, running on a Windows 10 64 bit OS.
I also have a Python 2.7 in my C: drive
And I am using PyQt5-5.4.1-gpl-Py3.4-Qt5.4.1-x64
Thank you for your time.
Maya's python interpreter is in the 2.7 series (or 2.6 for Maya 2013 and earlier). Your PYTHONPATH is pointing at a python 3.4 install. You also want to make sure that if your Python 3.4 is in PATH it comes later than the maya python install location. This is all to makes sure that Maya doesn't get confused and try to run python 3 code or dlls which it cannot handle.
Also, maya 2016 is running on PyQT4. Maya 2017 runs on PyQT5. So you'll probably want to use a PyQT4 version of designer
Related:
UIC / PyQT4 example
using UIC files in Maya pyQT 4
re: 2017/QT5

interactive mode doesn't switch on from script (matplotlib 1.4.0 python 2.7.5 on mac osx 10.8.5)

Using python 2.6 on my mac the following works fine (i.e. a plot window opens):
import matplotlib.pyplot as plt
from numpy import linspace, sin, pi
plt.ion()
print "Is interactive:?", plt.isinteractive()
x = linspace(-pi, pi, 1001)
plt.plot(x, sin(x))
raw_input() #keep the window open
It works when I run it in shell (i.e. $ python test.py) as well as when I run it in an interactive python terminal.
I recently installed python 2.7 and with it nothing happens (more precisely, plot window appears in Dock, but doesn't open) when I run my script from shell. The value of plt.isinteractive() is false even after plt.ion().
When I run the same code in an interactive python terminal, everything is fine.
The answer to this question makes the plot window appear, but I find it annoying that now I have to add plt.pause(0.1) to my script.
Is there a way to get the earlier behaviour without modifying the code?
The backend is macosx.
It seems that this is a bug related to matplotlib 1.4. An ugly workaround is to include:
import sys
sys.ps1 = 'SOMETHING'
before importing matplotlib.
Alternatively, one can use ipython to run the script.
For more details see here https://github.com/matplotlib/matplotlib/issues/3505

How do I make my ttk widgets look modern?

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
Instead of this:
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.
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
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

Resources