Problem with destroy in tkinter under Anaconda Spyder - anaconda

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.

Related

Pyside6/Qt6 crash in MacOS

The following program opens a window with a button that displays which version of PySide is in use. If you click the button, the program displays Wikipedia's home page.
It works fine on PySide 6.3.
On PySide 6.4, it works fine, UNLESS you maximise the window before pushing the button. If you do that, it segfaults.
I'm running Python 3.9 on MacOS Monterey.
import PySide6 as qt
import PySide6.QtWidgets as qtw
import PySide6.QtWebEngineWidgets as qtweb
app = qtw.QApplication([])
def showWeb():
stack.setCurrentWidget(web)
web.setUrl("https://en.wikipedia.org")
button = qtw.QPushButton(qt.__version__)
button.clicked.connect(showWeb)
web = qtweb.QWebEngineView()
stack = qtw.QStackedWidget()
stack.layout().addWidget(qtw.QWidget())
stack.layout().addWidget(web)
window = qtw.QWidget()
window.setLayout(qtw.QVBoxLayout())
window.layout().addWidget(button)
window.layout().addWidget(stack)
window.resize(400, 400)
window.show()
app.exec()
The error message on the zsh terminal looks like this:
zsh: segmentation fault python3 segfault.py
[... my command prompt ...] % [67231:259:1105/160207.214403:ERROR:mach_port_rendezvous.cc(310)] bootstrap_look_up org.chromium.Chromium.MachPortRendezvousServer.1: Permission denied (1100)
[67231:259:1105/160207.214737:ERROR:child_thread_impl.cc(226)] Mach rendezvous failed, terminating process (parent died?)

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

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.

use tensorflow on pyCharm

I am new to Deep Learning and I have just installed tensorflow on my Mac,
however, there are some problems.
I am sure I installed successfully because I can run tensorflow on Terminal using python 3.5
import tensorflow as tf
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
print(node1, node2)
there is an output on my bash
Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)
However, when I run import tensorflow as tf on Python Console of PyCharm. A problem occurred:
>>> import tensorflow as tf
ImportError: No module named 'tensorflow'
I just went through this a few days ago after switching to PyCharm. You have to select the right Project Interpreter so it can find the Python installation that includes Tensorflow. In the menu go to PyCharm->Preferences and under the section Project: PyCharm on the left select Project Interpreter. In my case, I select 3.5.2 (~/anaconda/bin/python) because there's where my Tensorflow seems to reside. Whatever versions you select, scroll through the window at the bottom to make sure you see Tensorflow listed as a Package.
IMPORTANT: in my case it seems that once you OK this, it takes a while for PyCharm to digest? Because for a while the 'Run' button is greyed out and you can't run your program. Took me a while to figure what I'm doing wrong, but looks like you just need to give it sometime and it picks it up and the Run button goes back to green. Hope this works for you...
I am using the Community Edition, for what it's worth...
I'm using PyCharm 2018.1.4 Community Edition on Fedora 28, and have TensorFlow 1.9.0 installed (through "sudo pip3 install --upgrade tensorflow"). Here is my step to load TensorFlow
File > Settings > Project > Project Interpreter > Change to "/usr/bin/python3.6" in Project Interpreter, then the installed Package will be loaded and displayed.
Good luck.

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