In this video tutorial I’ll explain how to use the Tkinter module for designing Python GUI applications. I specifically teach you how to setup:
This tutorial is part 1 of my series on using Tkinter with Python.
Like always, a lot of code follows the video. If you have any questions or comments leave them below. And, if you missed my other Python Tutorials they are available here:
[adsense]
Here is All the Code from the Video
from tkinter import *
import tkinter.messagebox
def beenClicked():
radioValue = relStatus.get()
tkinter.messagebox.showinfo(“You clicked”, radioValue)
return
def changeLabel():
name = “Thanks for the click ” + yourName.get()
labelText.set(name)
yourName.delete(0, END)
yourName.insert(0, “My name is Derek”)
return
app = Tk()
app.title(“GUI Example”)
app.geometry(‘450×300+200+200′)
labelText = StringVar()
labelText.set(“Click button”)
label1 = Label(app, textvariable=labelText, height=4)
label1.pack()
checkBoxVal = IntVar()
checkBox1 = Checkbutton(app, variable=checkBoxVal, text=”Happy?”)
checkBox1.pack()
custName = StringVar(None)
yourName = Entry(app, textvariable=custName)
yourName.pack()
relStatus = StringVar()
relStatus.set(None)
radio1 = Radiobutton(app, text=”Single”, value=”Single”, variable = relStatus, command=beenClicked).pack()
radio1 = Radiobutton(app, text=”Married”, value=”Married”, variable = relStatus, command=beenClicked).pack()
button1 = Button(app, text=”Click Here”, width=20,command=changeLabel)
button1.pack(side=’bottom’,padx=15,pady=15)
app.mainloop()
Nice tutorials 🙂
thank you!
it would be very useful to be able to download the scripts directly somehow.
Copy paste is a mess. actually it’s still not working. I have to rewrite everything from scratch.
Success
Thank you. I’m glad you like them. I made a zip archive of every file in the entire tutorial. It can be found on this page http://www.newthinktank.com/2010/10/python-2-7-tutorial/
If you would be so kind to click on the Google +1 button on that page, that would help others find that information in the future.
It would also make me happy, because that 0 is very depressing to look at 🙂
If you need anything else just ask. Thanks Derek
Of course!!:D
….
Done!…
now it’s +1 😀
at least this I can do!
Thank you! All I want is the opportunity to help more people 🙂
Dear Admin,
What can be a problem if your program works perfect except
import tkinter.messagebox?
The mistake is:
Exception in Tkinter callback
Traceback (most recent call last):
File “C:\Python27\LIB\LIB-TK\Tkinter.py”, line 1410, in __call__
return self.func(*args)
File “C:/Python27/Work_py/Window/simpleWindow.py”, line 7, in beenClicked
tkinter.messagebox.showninfo(“You clicked”, radioValue)
AttributeError: ‘module’ object has no attribute ‘messagebox’
Traceback (most recent call last):
File “C:/Python27/Work_py/Window/simpleWindow.py”, line 2, in
import Tkinter.messagebox
ImportError: No module named messagebox
I’ve installed Tkinter and windows and buttons work perfect except messagebox…
Thanks in advance.
Grate tutorial by the way!!
Try getting rid of import tkinter.messagebox all together. If you imported the tkinter module you don’t really need it anyway. I was just showing how to import the message box all by itself.
Thanks a lot.
But it doesn’t work any way..
What doesn’t work?
Derek,
I did as you instructed above and when I click the Single/Married radio button I get this message in the console:
Exception in Tkinter callback
Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py”, line 1410, in __call__
return self.func(*args)
File “/Users/___________________ /Documents/workspace/New Test/src/NewTest.py”, line 8, in beenClicked
tkinter.messagebox.showinfo(“You Clicked”, radioValue)
AttributeError: ‘module’ object has no attribute ‘messagebox’
I’ve been hearing this from a few people. It seems that you need to update your version of Pydev to fix this issue. Just click on Help and Check for Updates. That has fixed the problem for other people. I hope that helps
This is the basics on mine might help I hope
from Tkinter import *
import tkMessageBox
app = Tk()
app.TItle =”Widget”
app.geometry = “300,400”
app.mainloop()
Thank you for the extra information 🙂
http://www.tcl.tk/man/tcl8.5/TkCmd/contents.htmits for the new Tk you probably know this by now lol 🙂
That’s the problem with doing programming tutorials. If there is a major change to the core language or a core library all of the code can go wacky. That has kind of forced me to stick to simple tutorials.
Great tutorial, so much info. I have watched the video numerous times and it has helped me put a nice little GUI together. I am wondering if you know how to make a text box that allows one to drag and drop a folder in order to get the complete path to that folder. Basically, a text box that acts like the terminal does when dropping a folder onto the command line on a Mac. Thanks!
I’m not certain how that is done. I’ll look into it. Thanks for the idea
okay, great. If you figure it out. Can you please email me? I checked the email box below…
I have very big problem when i’m trying to run it
it replays that I don’t have the modules so what supposed I do??
^LOL!
Hi if you want to create a Menu on a Frame that already has buttons and other widgets on it, can you explain that please.
e
Thanks for the tutorial, in my computer labelTExt.set(name) doesn’t work. Any idea?
these are the best tutorial videos i ever seen,,im doing computer science first year at university of western cape and they help me get along with my course.
thanks upload more
Thank you very much 🙂 I’m very happy that I’m able to help
Dear admin
Great tutorials up to 9# 10-15 seemed hard to learn when and
where. Im confused but I love python and I made great things from your first 9 tutorials. I made progress until session 10?
What happened ?
matt
Feel free to post your questions. I get to all of them eventually.
Dear admin
Best teacher I found so far up to 10th tutorial?
matt
Thank you very much Matt 🙂