Python Forum

Full Version: my simple window code is not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
below you can find my code and the output, I am using PyCharm and Python 3.

Thank you,

code :
from tkinter import *

class Window(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master

root = Tk()

app = Window(root)

root.mainloop()
Output
ERROR :
Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/untitled/test.py", line 1, in <module>
from tkinter import *
ImportError: No module named tkinter
It almost sounds as if there is a problem with the environmental variables. Are you able to import other modules with out any errors (for instance "import math")?

If not, try typing in the command terminal (not PyCharm):
C:\PATH
You should see at least two entries, "C:Python(3)" and "C:Python(3)\Scripts". Note the actual directory and directory name may be different, depending on where you installed Python.

Just so you know, your script runs fine for me, using Python 3.6.4 and PyCharm.
I agree with @sparkz_alot. The script works fine on PC too.
i fixed it finally, the problem was in the project interpreter that python 2 was selected.

anyway, thanks a lot !
That seems strange, do you also have Python 2.x installed?

In any case, glad you fixed your problem. :-)
yes i have version 2 installed as well .. shall i better uninstall it ?
If you have some reason to have v2, then no. If you don't then yes, remove it. It will make your life easier. The v2 was probably the first one you installed, making it the default, which is why PyCharm automatically chose it. Also, if you use 'pip' it will be looking at the v2 library's, not the v3, you would need to use 'pip3' or some variation of that in order to affect the v3 library's. Without a "shebang" line, double clicking a .py (.pyw) file will try and run the file using v2. Similarly, typing "python file_name" will use v2, whereas "python3 file_name" will use v3. Another thing to remember, is if you type "python" in the command terminal you enter the v2 python shell, typing "python3", will invoke the v3 shell.

As you can see, there are some things you need to remember when you have two or more versions installed. As long as you remember the "gotcha's" you'll be fine. Linux users do it all the time as they usually come with v2 and v3 pre-installed.
i did. thanks for the helpful information!
No need to uninstall python 2.
PyCharm will default to last selected project interpreter unless one is specifically selected.
I have several, and use all without a problem.
When a new project is added, do the following:
  • file-->settings-->Project Interpreter
  • If you see the interpreter that you want in the pulldown, select it and you're done. It will be used from now on for this project.
  • If you need to add a new one, click on the gear icon (right of icon)
  • select add local
  • Choose your environment on left (virtualenv, conda, or System Interpreter)
  • Next click on new environment or existing environment
  • If new environment, choose (or add) base Interpreter (be careful on virtual environment, do not choose an interpreter that was created by another project unless you are sure this what you want to do, otherwise you will build a dependency).
  • in virtual, you can also choose to inherit all site packages (Normally don't do this, or you will dramatically increase the package footprint).
  • If not creating a virtual environment, choose existing environment
  • Select the Interpreter from pull down, or click ... ad navigate to the desired one, and add it.

Sounds like a lot, but it can do a lot, and ease creation of the virtual environment (if using one), and selection of packages by selection from pulldown and installing here.