Python 2.7 Tutorial Pt 15

PythonIn this video tutorial I’ll explain how to use the Tkinter module for designing Python GUI applications. I specifically teach you how to setup:

  • The Interface
  • Tkinter Labels
  • Tkinter Buttons, Radio Buttons and Checkboxs
  • Accept User Input
  • Call Python Methods with Event Handling

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()

27 thoughts on “Python 2.7 Tutorial Pt 15”

  1. 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

    1. 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

  2. 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!!

    1. 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.

          1. 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’

            1. 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

              1. 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()

                    1. 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.

  3. 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!

        1. 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??

  4. 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

  5. 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

  6. 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

Leave a Reply

Your email address will not be published. Required fields are marked *