I am also trying to understand how to use Tkinter so could you please explain the basics?
What is the difference between the _tkinter and tkinter modules?
_tkinter is a C-based module that exposes an embedded tcl/tk interpreter. When you import it, and only it, you get access to this interpreter but you do not get access to any of the tkinter classes. This module is not designed to be imported by python scripts.
tkinter provides python-based classes that use the embedded tcl/tk interpreter. This is the module that defines Tk, Button, Text, etc.
Related
My tkinter app will run console-free (.pyw) until I import pyttsx3. As soon as pyttsx3 is imported, the app will only run from the editor (Idle).
This is a tkinter app that runs perfectly when run from idle. I import pyttsx3, initialize it, have it speak using Windows Sapi voices, all is well, all tkinter functions operate as intended from start to finish. But outside of Idle, the app won't run in .pyw mode. It shows a black console screen for a brief moment and closes. I have checked very carefully - removing all pyttsx3 code from the app - except the import statement and, quite literally, the import statement alone is enough to cause the app to no longer run in .pyw mode.
import tkinter as tk
(runs fine in .pyw mode)
import tkinter as tk
import pyttsx3 as speak
(will not run in .pyw mode)
The question: how could simply importing a library (not even initializing or using it...just importing it) cause the tkinter app to no longer run as .pyw? Could importing a library somehow be interfering with the tkinter main loop?
Good question. If I had to guess, something in the pyttsx3 library invokes a process that is unrelated to Python, for the purpose of text-to-speech conversion. Windows probably opens a Command Prompt window in such a case since that process runs independently.
Unless the pyttsx3 library has documentation for how to suppress this—after a cursory glance, I don't see such—then I would recommend opening a new issue with the package maintainer. I believe that it would need to set the CREATE_NO_WINDOW flag when being run on Windows.
I have much experience with Python, but I'm just now learning Tkinter and the following code isn't working:
root = Tk()
root.mainloop()
It spits out the error message
"NameError: name 'Tk' is not defined"
It seems you are simply not importing the tkinter library.
The quick solution is to add from tkinter import * to the top of your file.
However, global imports are generally a bad idea. I know lots of tkinter tutorials start out this way, but they shouldn't. I recommend doing it this way:
import tkinter as tk
root = tk.Tk()
root.mainloop()
It requires that you prefix every tkinter command with tk., but it makes your code easier to understand, and easier to maintain over time. If, for example, you decide to import ttk (some modern looking tkinter widgets), it is impossible to know if Button(...) refers to the ttk button or the tk button if you use global imports. However, tk.Button(...) and ttk.Button(...) are crystal clear.
The error occurred because the file was named as tkinter.py and caused the tkinter library import to fail.
Make sure your file name differs.
I'm having trouble making a Sublime Text 3 plugin. I just installed wxPython with Python 2.7 on my Macintosh.
In the terminal, my Mac can find wxPython (import wx). But the source code of a Sublime Text plugin cannot import wxPython.
You can check out the screen capture below.
How can I fix this problem?
Plugins are executed using Sublime's internal Python interpreter, not any version of Python installed on your computer. Nearly all of the standard library is included, but a few packages (including Tkinter, among others) are not. To my knowledge it is not possible to use pip, for example, to install 3rd-party modules into Sublime Text.
However, if you would like to include some 3rd-party code, just put it in your plugin's directory. For example, if you store your plugin code in Packages/MyPlugin (where Packages is the directory opened by selecting Preferences -> Browse Packages...), and you want to include the 3rd-party library foobar, just copy the foobar directory into Packages/MyPlugin. Then, in your plugin code, use the following template, assuming you're trying to code for both ST3 (Python 3.3) and ST2 (Python 2.6):
try: #ST3
from .foobar import mymodule
except ImportError: #ST2
from foobar import mymodule
Obviously, if you're just planning on supporting ST3 (there are enough differences in the API to make programming for both versions annoying), you won't need the try/except clause. Also, if you are going to be distributing your plugin via Package Control or some other method, make sure you can redistribute the 3rd-party code, and that your license is compatible with its license.
I just noticed, when I was importing a file from my project I'm working on, that ipython3 is a little bit confused, because the file imports pyqt5 stuff.
Installed are both pyqt4 and 5, because have to use older versions from colleagues, who didn't upgrade their stuff yet.
My projects use pyqt5 so, how do I link ipython3 qtconsole --pylab=qt& to pyqt5 on default?
The error message was:
2
3 import sys, os, math, shutil, re
----> 4 from PyQt5 import QtCore, QtGui, QtWidgets
5 from ui_IMEX import Ui_IMEX
6
RuntimeError: the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class
which I interpret as pyqt4 is connected on default... because it's there.
Cheers,
Christian
Till yesterday I tried to figure out how to get ipython (versions 2.3.1 and 3.xdev) to accept --pylab=qt5 (or similar: qt5agg, qt5Agg) but no luck. If one takes a look at the documentation the kernel directives mention them but the code doesn't. I noticed that some parts aren't even python3 ready and having issues with hte print() statements by lacking the brackets.
For support, I'm adviced to look here but it seams that it isn't any issue here and the once who tried to help aren't working with that versions or didn't get that deep into ipython to come across.
It is damm onerous having to start and stop the program I'm working on just to see that it's not doing what I want. Debugging that way is most unpleasant and that's why iypthon is in so much use for developing.
So, aren't there any more ideas how to fix that?
I am using Qtdesigner3 to write a python gui that will read data files do some math and then plot the results using matplotlib. I've looked at examples of matplotlib qt, but none of them use the designer.
My question is how do I create a matplotlib widget in the QTdesigner?
There is a better response to almost this exact question over at the pythonxy mailling list
http://groups.google.com/group/pythonxy/browse_thread/thread/c52424bf11328181?pli=1
To quote the salient point from the thread. This is from one of the lead developers of the project.
You may directly use the Matplotlib Qt Designer plugin which is
shipped with Python(x,y).
It is located here: "C:\Python26\Lib\site-packages\PyQt4\plugins
\designer\python".
On your linux machine, create an environment variable called
PYQTDESIGNERPATH, and set it to the directory containing
'matplotlibplugin.py'. That's all!
Python(x,y) has a matplotlib widget that can be used in Qt Designer. If you don't want to install Python(x,y), you can follow the instructions at this tutorial to create your own.