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 Responses to “Python 2.7 Tutorial Pt 15”

  1. Mihai says:

    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

  2. Leonid says:

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

    • admin says:

      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.

      • Leonid says:

        Thanks a lot.

        But it doesn’t work any way..

        • admin says:

          What doesn’t work?

          • Jeremi Bauer says:

            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’

  3. Jason says:

    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!

  4. Arch says:

    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. Bill Wilder says:

    Thanks for the tutorial, in my computer labelTExt.set(name) doesn’t work. Any idea?

  6. siyanda says:

    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

  7. matt winchester says:

    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

  8. matt winchester says:

    Dear admin
    Best teacher I found so far up to 10th tutorial?
    matt

Leave a Reply

Your email address will not be published.

Google+