Please ignore the rest of the code, except for the highlighted part (or the line 'ent1=Entry(topf, width=25)' to line 'ent1.insert(INSERT, wrong, if you cannot see the color). You can copy this into Python and make sure you have the GIF file in the same dir. Also, make sure that you have PIL installed. The only radio button that currently works is Alabama.

I cannot get this to work. I get an error after running it after selecting "Take the quiz" for Alabama, and what I'm trying to do it to evaluate what the person enters in the box for the capital of Alabama. I want the user to enter the capital and if it's not right, I want the program to tell them within that box. Apparently, Python does not like the " 0.0" entry within the box.
 
The error in Python is 
 
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args)
  File "C:\Documents and Settings\Student\Desktop\map- test.py", line 98, in stateget
    ent1.delete(0.0, END)
  File "C:\Python24\lib\lib-tk\Tkinter.py", line 2307, in delete
    self.tk.call(self._w, 'delete', first, last)
TclError: bad entry index " 0.0"
 
Why is this not working? Here's the code:

from Tkinter import *
import ImageTk
class Gui(Frame):

def lab(self):
  # A generic text label for the frame
  self.lab1 = Label(base, text = "Text for label") #text will be reset later
  self.lab1.grid(columnspan=3)
    
def but(self):
  # A generic button for the frame
  self.but1 = Button(bottom, text="Take the quiz") #text and command to be set later
  self.but1.pack(side=RIGHT)
  
def labR(self):
  # A generic text label for the right frame
  self.lab1R = Label(R, text = "Text for label") #text will be reset later
  self.lab1R.pack(side = TOP)
    
#def butL(self):
  # A generic button for the left frame
  #self.but1L = Button(L, text="text on button") #text and command to be set later
  #self.but1L.pack(side = LEFT)
  
def butR(self):
  # A generic button for the right frame
  self.but1R = Button(RB, text="text on button") #text and command to be set later
  self.but1R.pack(side = LEFT)
    
#def entcap(None):
  # A generic text entry for the left frame
  #self.entcap1 = Entry(width=25) #width can be changed later
  #self.entcap1.pack(side = BOTTOM)
  
def entR(self):
  # A generic text entry for the right frame
  ent1R = Entry(RB, width=20) #width can be changed later
  ent1R.pack(side = TOP)

def txtL(self):
  # A generic text box for the left frame
  self.txt1L = Text(L, width=100, height=5, wrap=WORD)
  self.txt1L.pack(side = BOTTOM)

def txtR(self):
  # A generic text box for the right frame
  self.txt1R = Text(R, width=100, height=5, wrap=WORD, padx=5, pady=5)
  self.txt1R.pack(side = TOP)
  
def rdbut(self):
  
  self.states = StringVar()
  Radiobutton(base, text="Alabama", variable=self.states , value="AL").grid(row=20, column=0, sticky=W)
  Radiobutton(base, text="Arkansas", variable=self.states, value="AK").grid(row=20, column=1, sticky=W)
  Radiobutton(base, text="Florida", variable= self.states, value="FL").grid(row=20, column=2, sticky=W)
  Radiobutton(base, text="Georgia", variable=self.states, value="GA").grid(row=25, column=0, sticky=W)
                Radiobutton(base, text="Kentucky", variable= self.states, value="KY").grid(row=25, column=1, sticky=W)
                Radiobutton(base, text="Louisiana", variable=self.states, value="LA").grid(row=25, column=2, sticky=W)
                Radiobutton(base, text="Mississippi", variable= self.states, value="MS").grid(row=30, column=0, sticky=W)
                Radiobutton(base, text="North Carolina", variable=self.states, value="NC").grid(row=30, column=1, sticky=W)
                Radiobutton(base, text="South Carolina", variable= self.states, value="SC").grid(row=30, column=2, sticky=W)
                Radiobutton(base, text="Tennessee", variable=self.states, value="TN").grid(row=35, column=0, sticky=W)
                Radiobutton(base, text="Virginia", variable= self.states, value="VA").grid(row=35, column=1, sticky=W)
                Radiobutton(base, text="West Virginia", variable=self.states, value="WV").grid(row=35, column=2, sticky=W)

    

        def stateget(self):
                state = self.states.get()
                if state == "AL":
                    top = Toplevel()
                    top.title("Alabama Capital Quiz")
                    topf = Frame(top)
                    topf.pack()
                    topb = Frame(top)
                    topb.pack()
                    pct = ImageTk.PhotoImage(file="AL.gif")
                    var = Canvas(topb, height=250, width=250)
                    img = var.create_image(10, 10, anchor=N, image=pct)
                    var.pack()
                #top.geometry("500x500")
  # Now I add a text box
  #txtbx2 = Text(topf, height=5, width=40, bg="yellow", wrap=WORD)
  #txtbx2.pack(side=TOP)
  #txtbx2.insert(INSERT, message2)
                    close = Button(topb, text="Close", command= top.destroy)
                    close.pack(side=RIGHT)
                    ent1=Entry(topf, width=25)
                    ent1.insert(INSERT, "What is the capital of Alabama?")
                    ent1.pack()
                    name = ent1.get()
                    right = "Correct!!!"
                    wrong = "No, please try again."
                    if name == "montgomery":
                        ent1.delete(0.0, END)
                        ent1.insert(INSERT, right)
                    else:
                        ent1.delete(0.0, END)
                        ent1.insert(INSERT, wrong)
                      
  
root = Tk()
root.title("US Southeast Regional Quiz")


gui = Gui(root)


base = Frame(root)
base.grid()

gui.lab()
gui.lab1.configure(text="Welcome to the Southeast US State Quiz!")

# I will bring in another label explaining what I want
gui.lab()
gui.lab1["text"]="Please select a state to begin and click on the 'Take the quiz' button below."

# Now for the radiobuttons displayed with the updatemsg textbox
gui.rdbut()
#gui.updatemsg()

# I Need a frame for my buttons
bottom = Frame(base)
bottom.grid(row=99)

# To get out of the program
gui.but()
gui.but1.configure(text="Exit", command= gui.quit)

# To run the msg function
gui.but()
gui.but1.configure(text="Take the Quiz", command=gui.stateget)

      
root.mainloop()

 

<<attachment: AL.gif>>

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to