Python Forum
[Tkinter] my simple window code is not working
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] my simple window code is not working
#1
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
Reply
#2
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
I agree with @sparkz_alot. The script works fine on PC too.
Reply
#4
i fixed it finally, the problem was in the project interpreter that python 2 was selected.

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

In any case, glad you fixed your problem. :-)
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
yes i have version 2 installed as well .. shall i better uninstall it ?
Reply
#7
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
i did. thanks for the helpful information!
Reply
#9
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 486 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  Tk .set not working in parts of code User01 2 1,874 Feb-13-2021, 03:39 PM
Last Post: deanhystad
  [Tkinter] On Win 10, no graphical window when run code ma2020uk 10 5,794 Nov-02-2020, 02:21 PM
Last Post: Larz60+
  Closing window on button click not working kenwatts275 4 3,747 May-03-2020, 01:59 PM
Last Post: deanhystad
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 3,750 Dec-16-2019, 04:47 AM
Last Post: woooee
  tkinter window and turtle window error 1885 3 6,714 Nov-02-2019, 12:18 PM
Last Post: 1885
  Code not working Luzaich 2 2,819 Sep-03-2018, 04:37 PM
Last Post: Gribouillis
  update a variable in parent window after closing its toplevel window gray 5 9,075 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020