Can't make a tk image Button with IPython Notebook - image

I'm using an IPython notebook to create a Tk application in Python. I can make a button with a text label:
%matplotlib tk
import tkinter as tk
root = tk.Tk()
button = tk.Button(root, text = 'IPython')
button.pack()
But if I try to make a button with an image I get an error message. This code works fine when run from the command line, but crashes when executed in an IPython Notebook:
%matplotlib tk
import tkinter as tk
root = tk.Tk()
img = tk.PhotoImage(file='IPy_header.gif')
print(img)
button = tk.Button(root, image = img)
button.pack()
The print statement verifies the PhotoImage object was created (i.e. no 'file not found' errors). The call to tk.Button leads to this error:
TclError: image "pyimage2" doesn't exist
I'm using ipython/jupyter 3.0.0 on Mac OS X 10.10.3
Anybody have a suggestion?

You may have to enable the tk event loop with %gui tk
magic-gui documentation.

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?)

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.

jupyterlab button event not working

This code work in jupyter notebook, but doesn't work in jupyterlab:
import ipywidgets as widgets
from IPython.display import display
button = widgets.Button(description="Click Me!")
display(button)
def on_button_clicked(b):
print("Button clicked.")
button.on_click(on_button_clicked)
Does anyone have a solution ?
Env:
MacOsX 10 10.12.2
Python 2.7.14 :: Anaconda, Inc.
Jupyter Notebook 5.4.0
Jupyter Lab 0.31.5
Current, still know as issue ... but here I found a solution.
import ipywidgets as widgets
button = widgets.Button(description='Display Chart')
out = widgets.Output()
def on_button_clicked(b):
button.description = 'clicked'
with out:
print('Ay')
button.on_click(on_button_clicked)
widgets.VBox([button, out])

ImportError: cannot import name QtGui or any other PyQt4 module

I´m using PyQt4 with Mac Yosemite 10.10.5. and Python 2.7. When I try to run a simple script using sublime text I get
´ImportError: cannot import name QtGui.
However when I go into python through the terminal and enter the following line, it seems to accept it.
>>> from PyQt4 import QtGui
>>>
The script I´m trying to run in Sublime Text is:
import sys
from PyQt4 import QtGui
def main():
app = QtGui.QApplication(sys.argv)
w = QtGui.QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
Main()
My Pyqt4 path is at /Library/Python/2.7/site-packages.
My sys-path output is:
['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Users/cameronmacintyre/Library/Python/2.7/lib/python/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']
I installed using sip and PyQt4 was installed with the following:
python configure.py -q /usr/bin/qmake-4.8 -d /Library/
Python/2.7/site-packages/ --use-arch x86_64
•
make
•
sudo make instal
I´ve searched other similar problems for days and can´t find a solution. I´m a bit of a newbie to this, so any help would be appreciated.
ps I did have Python 3 installed for a while but I removed it.

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