tkinter in Spyder - python

I am trying my first steps in tkinter. I use Spyder as IDE in Python 3.5.1 |Anaconda 4.0.0.
I want to run the very simple script below but it always crashes my Spyder. In a normal shell/bash it runs though and opens the canvas.
import tkinter as tkr
tk = tkr.Tk()
canvas = tkr.Canvas(tk, width=500, height=500)
canvas.grid()
tk.mainloop()
Under Preferences for the Ipython Console I already tried different settings (i.e. Qt, Automatik, Tkinter) but none of it did help.
What am I doing wrong (and how can I do it better)?
many thanks in advance

update to Spyder 3.0.1
https://pythonhosted.org/spyder/
https://github.com/spyder-ide/spyder/releases/tag/v3.0.1
I just did this on win 10: no crash, got blank "tk" separate window

You should try to change the graphics backend
Go to tools/preferences/I-python Console/Graphics and in backend change it to Tkinter.
That should do it!!

In the Spider menu bar, go to: Tools > Preferences
A window will open, then on left side go to: Completion and linting
In the right side, go to: introspection and below you will see different modules available in your current spider
In the section "Preload the following modules...", add tkinter to the end of the list

I found this suggestion while trying to get tkinter to display a GIF image in a label. First, on a Mac, the Tools menu item does not contain Preferences--look under the "python" menu instead. Even so, I have the
IPython Console Graphics backend set to Tkinter and tkinter is added to Completion and Linting. But all I get when launching from Spyder is an empty frame entitled "tk #442", whereas when I launch from the command line I get a frame with a nice GIF image as expected.

Related

How to open a Pygame window and a Tkinter window simultaneously?

I'm making a simple game using the Pygame Module. I require a Tkinter window to be open alongside the Pygame window.
Whenever I try to open both the windows, the second window only opens after I kill the first one.
Now, the only solution I can think of, is using multi-threading. But, I'm not able to implement it.
How do I go about this?
I would really appreciate some help here.
Thank You!
There is a fundamental design issue in pygame that makes it unable to open a window if the process already has a window. It will also prevent other windows from opening while it is running. However, you can open as many TK windows as you like and you can embed a pygame window inside a SDL drawing frame inside a TK window.
See this answer on how to use the drawing frame: Embedding a Pygame window into a Tkinter or WxPython frame
See this answer on how to create multiple windows in tkinter: How to open multiple windows in Tkinter

Focus on pygame window

I am using
import ctypes
ctypes.windll.user32.ShowWindow( ctypes.windll.kernel32.GetConsoleWindow(), 6 )
to close the console and leave the pygame window open however I have to click on the window to interact with it. Can I make it so it goes back to the pygame window automatically?
A python script may be run in windowed mode by launching with pythonw, this may be automatically handled by changing the file extension to .pyw.
See this answer for more detailed information.

pyqt how to make application truly active

Under OS X, using the zetcode example for menubar, if one runs this from the command line:
$ python menubar.py
the application starts, but at the bottom of the window stack. This can be fixed by adding
self.raise_()
to the code.
However, the application is still not immediately active. The terminal application menubar still shows, not the pyqt menubar.
The only way to get the pyqt menubar to show is to switch away from the pyqt application or terminal application and back again.
Is this expected behavior? Is there anyway to fix this so the pyqt application immediately becomes active, i.e. the pyqt menubar immediately shows upon execution.
Versions: OS X 10.9, Qt 4.8.6, PyQt 4.11.3, SIP 4.16.5, python 2.7.8
The identical problem affects some apps bundled by PyInstaller. The following blog post discusses the fix, however the fix is in terms of the PyInstaller boot loader, which would not be directly relevant to your code, but it might be informative.
http://dvitonis.net/blog/2015/01/07/menu-bar-not-visible-when-building-pyqt-app-bundle-pyinstaller-mac-osx-mavericks-yosemite/
The problem relates to use of TransformProcessType() which is discussed in this linked article:
http://www.sheepsystems.com/developers_blog/TPT-show-menu.html
I do not know what Qt method would relate to the Mac OS TransformProcessType() but you might experiment with calling QWidget.setWindowState() in your mainwindow's __init__().
I was experiencing this behavior, but only when I launch with a --nosplash option to skip the splash screen in my application.
That gave me a hint, and I developed this workaround using a 'dummy' splash screen:
widget = QtGui.QMainWindow() # or whatever your main window class is
dummy = QtGui.QSplashScreen()
dummy.show()
dummy.finish(widget)
widget.show()
widget.raise_()

How to make PyQt App give up window focus?

I have a python command line script that starts a PyQt Application showing the input of the video device in a little window. Then it starts a curses screen so that the user can control the camera with the keyboard.
After starting the script, the window focus is automatically on the PyQt window. Is there any way to tell PyQt to give up the window focus or curses / the terminal to reclaim it?
I use Linux (Mint Cinnamon) and the solution does not have to be OS independent.
You can try with setFocusPolicy here is the documentation. http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#setFocusPolicy

build cmd into Tkinter window

Hi i was wondering if you could have the comand prompt box pop into the Tkinter window when you start the program? Some thing like:
from Tkinter import *
admin = Tk()
cmd = Cmd(admin)
cmd.pack()
admin.mainloop()
I'm on windows
http://tkinter.unpythonic.net/wiki/CmdTkHere is what you want, this is not just opening a cmd window. its embedding cmd.exe into a Tkinter.Frame. And a note here if you rename the python script to ".pyw" extension, the console will be hidden. Except for in a virtual environment's.
I don't believe there is any built in console widget. It may be possible to whip up a custom one using the Tkinter Text widget. However, that would take a bit of effort/time.
Another possible option is simply have your program launch command prompt.
Two different ways to launch command prompt on a Windows machine.
import subprocess, os
subprocess.Popen('cmd.exe')
os.system("cmd.exe")
EDIT:
Unforunetly I don't believe there is any built in widget like that. However I thought of another possible solution, check out the code for the IDLE GUI, it has a console and the GUI portion is entirley written using Tkinter. So you may be able to utelize that code.

Categories