GUI/Using Tkinter

I'm a novice python programmer, and am wanting to spend some time learning the gui side of things. Everything I read suggests using Tkinter. I know it is included with IDLE, but I am much much much happier using Pycharm.

Is there a way to use Tkinter in Pycharm? I've done some googling, etc and can't find a clear answer. It appears, based on just some very basic testing I've done, its either not usable, or I don't have something set up properly.

Any insight/guidance is appreciated.
14 comments
Tkinter works just fine from PyCharm.  One thing to be aware of, though, is that depending on whether you are using Python 2.x or 3.x.  For the former, you would need `import Tkinter`, whereas for the latter you would need `import tkinter` (note the change in capitalization).

After that... it depends some on what operating system you are using.  If you are running Python on say Windows where you had to install Python from scratch, or on a Mac, then tkinter probably came packaged along with python, being part of the 'standard library'.  If you are running Python on something else like Ubuntu Linux, then you might not have tkinter isntalled yet as they (Ubuntu, for example) sometimes split up the standard library bits because somethings (for instance, a GUI library like tkinter) aren't really necessary for basic minimal system operation and may take up un-necessary space.  To continue the Ubuntu example, you would need to install either the `python-tkinter` package or `python3-tkinter`, depending again on python 2.x (default) or python 3.x.

HTH,

Monte
1
Thanks. I knew about that change, but must have been messing up somewhere else, I think with an import statement. Long story short, it now works.
0

I'm using python 3.7, and I did from Tkinter import *, and when I wrote my code an pressed "Run", a screen popped up and shut down within half a second. This happens every time I press "Run". Can somebody please help me?

0

I'm completely new to Python, but ran into the same problem. From other souces I managed to add the following to get tkinter window to stay open: 

# Bit of code to keep tkinter window open in Pycharm
root = Tk()
root.mainloop()

Don't ask me how it works though. If someone can explain, or improve on this, I'd be grateful.
1

You will always require require the use of ".mainloop()" in any creation of a window within Tkinter, there's not really an alternative I know of. It is a function that cycles the window until it is interrupted with a command such as quit. The mainloop function always needs to be declared at the end of whatever is within the given window you have created or else it'll cut off anything that you tell the Python console to run within the window.

The variable you create "root" is what you will reference anything to, such as Labels, Entry windows etc as that is what is called the "master". The master tells the widget (such as a Label) which window to go to, it's like a GPS location, but only for Tkinter widgets. Frames come into play with the "master" but that's much more advanced, but keep it in mind as it's something to explore later on within Tkinter. 

Anyway, here are two examples to give you a bit of clarity of what I have explained, if you run them in your Python IDE of choice you'll be able to see the use of Mainloop, also I added a few bonus features to the window for you being completely new to Python, hope this clears some stuff up for you! :)

 

This is how it should look because Mainloop is at the end of everything declared for the window-

import tkinter
from tkinter import *
##Import of Tkinter module

window = Tk()
##Creates the window from the imported Tkinter module
window.geometry("600x400")
##Creates the size of the window
window.title("Test :)")
##Adds a title to the Windows GUI for the window

window.mainloop()
##Loops the window to prevent the window from just "flashing once"

 

This is how it shouldn't look, as Mainloop is placed in the middle of everything being declared for the window-

import tkinter
from tkinter import *
##Import of Tkinter module

window = Tk()
##Creates the window from the imported Tkinter module
window.mainloop()
##Loops the window to prevent the window from just "flashing once"
window.geometry("600x400")
##Creates the size of the window
window.title("Test :)")
##Adds a title to the Windows GUI for the window
1

Thank you so very much for that. Great explanation.

 

1

No problem! Also always remember to add the two brackets to the end of Mainloop like so- "root.mainloop()" as Mainloop is a function, not a command or else your program won't run but the Python IDE won't detect it as a fault in the console.

0

Try the simple one on windows

 

import _tkinter
from Tkinter import *
root = Tk()
w = Label(root, text="Hello World")
w.pack()

root.mainloop()
0

In Debian it work

sudo apt install python3-tk

0

Pycharm can not import tkinter library in windows Python3.7,is like pycharm can not find the library.

0

Hi, could you please provide an example? Please feel free to create a ticket in our bug tracker directly: https://youtrack.jetbrains.com/issues/PY

0

This is nuts. I can't find a way to get tkinter to work on pycharm on linux. I also can't get tkinter to work with Eric. I'm going to have to abandon creating a linux distribution of my program.

0

Please sign in to leave a comment.