#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Python GUI, Questions and Errors


    Hello,

    My goal is create a program that uses radio buttons to select background colors for text(I'm new to GUI).

    when I tried to run my program in python 3.2.3 I keep getting errors such as:

    NameError: global name 'changeColor' is not defined
    ImportError: No module named tkinter

    Why do I keep having these errors?

    [CODE]from tkinter import *

    class buttons:
    def __init__(self):
    window = Tk()
    window.title("Radio buttons and buttons")


    self.var = IntVar()

    w1 = Radiobutton(window, text="Red", variable=self.var, [CODE]value=1,command=changeColor).pack()
    w2 = Radiobutton(window, text="Yellow", variable=self.var, value=2,command=changeColor).pack()
    w3 = Radiobutton(window, text="White", variable=self.var, value=3,command=changeColor).pack()
    w4 = Radiobutton(window, text="Grey", variable=self.var, value=4,command=changeColor).pack()
    w5 = Radiobutton(window, text="Green", variable=self.var, value=5,command=changeColor).pack()
    B1 = Button ( window, text="<=", command=LEFT).pack()
    B2 = Button ( window, text="=>", command=RIGHT).pack()

    def changeColor(self):
    self.var.get()
    self.changeColor()



    window.mainloop()


    buttons()[CODE]
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Have you installed tkinter somehow? If not, that's why you cannot import it.

    Second, changeColor() is a class method, so it needs to be called with self.changeColor() .
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,698
    Rep Power
    480
    Module names changed with python version 3.
    tkinter is the python3 name.
    Tkinter is for python2.
    That is why I use in my codes
    Code:
    try:
        import tkinter
    except:
        import Tkinter as tkinter
    or sometimes I use a version test.
    sys.version[0] is either 2 or 3.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo