I am trying start learning Tkinter to make a small gui application, however every time I do import tkinter all the print statements are duplicated. The problem exists with the absolute simplest script.
I am running windows 8.1 and python 3.5, and I am writing and building the application in Sublime Text 3 with a customized build system.
It is however unlikely that ST3 has anything to do with it since the problem also exists when running the script from the command line py -3 tkinter.py
Here is an example of my problem. This is all of the code.
import tkinter
print("Hello")
Always outputs
Hello
Hello
But code consisting of just print("Hello") outputs, as expected
Hello
I am truly lost with this problem and it would be of great help if any of you could point me to the right direction
You see two prints as your named your script tkinter.py so the print shows when you run it with py -3 tkinter.py and you see another as you import again in the script. You are not importing from the tkinter lib but from your own script whose name shadows the tkinter lib.
To fix, rename your script and delete any .pyc files in the directory
Related
I'm new to using atom and was wondering how to run a python script on it. I have code written at the moment which works perfectly fine in normal python shell, using tkinter, however when I run it through the command line, it says:
import tkinter as tk
ImportError: No module named tkinter
How do I fix this? In my environment variables I have already added python.exe, the file directory to the actual script I'm running and to the python download itself. How do I fix this?
The best way is to load the Jupyter plugin. It's called Hydrogen. Then under the packages menu, you can select Hydrogen/Run all and it will run your python code. there is a keyboard shortcut for doing this which speeds up the process. You can easily check your code as you write it by using the hydrogen option to run a line and go to next line.
As for your Tkinter problem have you loaded Tkinter? You can do this using pip install Tkinter. After that try running your code again.
In python 2.7 and below, the modules's name is Tkinter, while in 3.0 and above, it is tkinter.
Atom (whatever this is) seems to use a 2.7 or below version of python interpreter.
I am using the tsl2561 python package on my RPi in order to interact with the tsl2561 light sensor. However, I am having a really weird occurrence that I cannot understand. Let me try to explain it the best I can.
If I open up IDLE and run
from tsl2561 import *
then when I try to run the TSL2561(debug=True) command it does not work and says TSL2561 is not defined.
However, when I open up the __init__.py file located in the tsl2561 folder (which contains only one line "from tsl2561 import *"), and run that file, it opens a new IDLE window upon where I can type
TSL2561(debug=True)
and it recognizes the function and allows me to see the light level. But, if I open IDLE again in a new window and run
from tsl2561 import *
TSL2561(debug=True)
I get the error saying TSL2561 is undefined.
So, my question is, if you understand it, why does the function work when I run the __init__ script which has the same line but not when I just open up IDLE and try to type the code myself?
Any help would be greatly appreciated because I need to be able to write a separate script without having to rely on always opening from the init file!
It is Python 3.4
Thanks,
Andrew
This is what i have tried till now is :
I have created a setup.py file and in this written the following code.
#setup.py
from distutils.core import setup
import py2exe
import time
setup(console=['myprogram.py'])
time.sleep(2)
then in command prompt i entered into working directory and run this command
**python setup.py install**
**python setup.py py2exe**
But when i click on the exe file the console window appears and vanishes soon. Can anyone suggest where i went wrong.
I am using python 2.7.6, IDLE.Advance thanks. I googled with no much help .
Is there anything to do with dll or the python path??? I may have done some changes.
My program consists of openCV code and tkinter code for creating GUI in python.
I thought time.sleep(2) would had been the problem , but its not the issue, i tried checking it.
The console window is your application. I don't know what it should do but it think that it vanishes because of some exception. Add a logging or something so you will be able to see what goes wrong there.
Can you please let me know how to download tkinter for windows 7 64bit. I am getting an error while i do it:
from Tkinter import *)
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
from Tkinter import *
ImportError: No module named 'Tkinter'
If you are using python 3 then the code would use a lower case tkinter.
#python 2
from Tkinter import *
#python 3
from tkinter import *
if that doesn't fix it then you can install it in several ways. check out this page: http://www.tkdocs.com/tutorial/install.html
If you can't get that to work another option is using pip. pip allows you to install modules easily. If you have the newest version if python 2 then it is installed and all you have to do is set a path. Click the start button and right click on computer. Select properties and then advanced settings. Under one of the tabs is a button called environment variables. Click on it and in the new windows scroll down until you see the word "path" on the right. Click on the text to the left of it and put in a ; to put in a new path. I don't know exactly how your file structure is but find python and set a path to the folder called scripts within python. An example would be something like this: "C:\python27\scripts".
There are two possible reasons that you are not able to get Tkinter working:
First Reason
First, this is dependent on if you have installed tk onto your computer. To find out if you have, and what version, run a Python shell via:
% C:\python32\python
From the Python command prompt, enter these two commands:
>>> import tkinter
>>> tkinter._test()
This should pop up a small window; the first line at the top of the window should say "This is Tcl/Tk version 8.5"; Or whatever version has been installed on your computer. If you haven't installed, you'll want to get tkinter on there. I've found the following that may help with that:
http://www.tkdocs.com/tutorial/install.html
Second Reason
If installing tkinter onto your machine isn't the issue, then this is most likely due to what version of python you are using, as this will change some small things.
Python 3:
from tkinter import *
Python 2.7:
from Tkinter import *
If you have python you are trying to run from 2.7 or before but are using it in Python 3, you may find the 2to3 tool helpful for adapting:
https://docs.python.org/2/glossary.html#term-to3
open IDLE Python GUI and type in import tkinter and after that type import_tkinter and if you wanna run a test just type in tkinter._test()
works for python 3.4.3
I have a tkinter script, which runs just fine in IDLE. However, when I double click the .py-file from Windows Explorer, the console window flashes half a second and then it exits.
I was able to screenprint the console window. It says:
...etc.etc...
NameError: global name 'simpledialog' is not defined
simpledialog is a module in tkinter which I use in my script. As I do from tkinter import *, there is no need to explicitly write tkinter.simpledialog.
It works in IDLE, why not as .py?
IDLE uses Tkinter as its graphical environment. It is possible that your code is relying on a side effect of an import by IDLE itself. This is especially true if you use IDLE without a subprocess.
The simpledialog module does not import when using from tkinter import *.
Try adding this to your code:
import tkinter.simpledialog as simpledialog
Have you updated your PATH environment variable so that your Python executable is found? You can find more information on how to do here - Using Python on Windows
But you basically need to make sure that the folder containing python.exe (e.g. C:\Python32) is displayed when you type the following command from a prompt:
echo %PATH%
I had exactly the same problem with one of my scripts utilizing Tkinter.
Adding call to mainloop() fixed the issue.
See this tutorial for an example: [http://sebsauvage.net/python/gui/#import1
In my case, in the init function I have
def __init__(self,Width=400, Height=400):
# Create GUI window ------------------------------
win = Tk()
...
in the end of init I added:
win.mainloop()
Now it works by just running the file.
Hope this helps
Similar trouble for me just now, in my first week with python. But I dimly remembered a similar problem with a simple early test script and thought the trouble then was # comments.
So I tried that with my Tkinter infused .py script. It ran fine in IDLE as you say, then only flashed when clicked in windows. But there were a couple # commented lines at the top of file.
I took them all out and it now runs no sweat directly in windows. Have a look .. for #.
Sorry, can't seem to delete this post. Now the files work #comments included. Don't know what's up with that. ..
I found that changing the executable py file to a file.pyw fixed the problem. This tells python to execute it using the pythonw.exe which runs the script without the terminal/console in the background.
Not sure why this works, perhaps some screwed up environment variables from a previous python installation.
Changing the file's extension to pyw instead of py might solve the problem