Class issue.

Reply

Join Date: Oct 2008
Posts: 5
Reputation: Astudent is an unknown quantity at this point 
Solved Threads: 0
Astudent Astudent is offline Offline
Newbie Poster

Class issue.

 
0
  #1
Oct 25th, 2008
So I am getting into the swing of python in my class, and am currently trying to take a program from an example, and making it much more condensed. The issue I am having, is there are many classes for a tkinter guessing game.

def question5():
        if entry5.get() in ["Samus", "samus", "Metroid", "metroid", "Prime", "prime", "Metroid Prime", "metroid prime"]:
               answerlabel5['text']= "Its the amazing Metroid game heroine Samus, good call!"
              
        else:
               answerlabel5['text']= "Sigh.. try again."

def question(butnum):
    print "calling proc" #shows space in output to see what is going in
    print "you submitted", butnum #trying to find what output is happening.
    """if (ent[i]).get() in ans[i]:
        (a[i])['text'] = acor[i]
    else:
        (a[i])['text'] = ahint[i]""" #" was an attempt to get it to work.
  
for i in range(0, 5):
    images[i] = Label(image=images[i]).pack()
    ent[i] = Entry().pack()
    sub[i] = Button(text="Submit Answer",command=(lambda: question('i')), fg="white",bg="grey50", font=('impact',10)).pack()
    ans[i] = Label(text="", bg="white", font=('impact',10)).pack()

The class question 5 is one of 5(obviously) that checks for a right answer. I was trying to make a class that would dynamically check based on what entry box I was entering into. How would I get that to work with the class, or is it even possible?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 666
Reputation: ZZucker is on a distinguished road 
Solved Threads: 38
ZZucker's Avatar
ZZucker ZZucker is offline Offline
Practically a Master Poster

Re: Class issue.

 
0
  #2
Oct 25th, 2008
I don't see a class in your code.
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 5
Reputation: Astudent is an unknown quantity at this point 
Solved Threads: 0
Astudent Astudent is offline Offline
Newbie Poster

Re: Class issue.

 
0
  #3
Oct 25th, 2008
Eh yah, sorry wasn't thinking straight when I made this post last night. What I meant was:

The procedure question 5 is one of 5(obviously) that checks for a right answer. I was trying to make a procedure that would dynamically check based on what entry box I was entering into. How would I get that to work with the procedure, or is it even possible?


Sorry for the confusion.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 5
Reputation: Astudent is an unknown quantity at this point 
Solved Threads: 0
Astudent Astudent is offline Offline
Newbie Poster

Re: Class issue.

 
0
  #4
Oct 27th, 2008
While I look stupid with the wrong title for this thread(mod help with edit button please?), I still have the issue with procedure condensing. Anyone have any ideas?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Class issue.

 
0
  #5
Oct 27th, 2008
When in doubt run a simplified test code:
  1. # test Tkinter Entry
  2.  
  3. import Tkinter as tk
  4.  
  5. def question5(event):
  6. if entry1.get().lower() in ["samus", "metroid", "prime", "metroid prime"]:
  7. label2['text'] = "Its the amazing Metroid game heroine Samus, good call!"
  8. else:
  9. label2['text'] = "Sigh.. try again."
  10.  
  11. root = tk.Tk()
  12.  
  13. # first entry with label
  14. label1 = tk.Label(root, text='Enter heroine:')
  15. label1.grid(row=0, column=0)
  16. entry1 = tk.Entry(root, bg='yellow', width=50)
  17. entry1.grid(row=1, column=0, padx=5)
  18. label2 = tk.Label(root, text=' ', fg='red')
  19. label2.grid(row=2, column=0, pady=5)
  20.  
  21. # start cursor in enter1
  22. entry1.focus()
  23. # value has been entered in enter1 now press return key to check content
  24. entry1.bind('<Return>', question5)
  25.  
  26. root.mainloop()
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 45
Reputation: tyincali is an unknown quantity at this point 
Solved Threads: 6
tyincali tyincali is offline Offline
Light Poster

Re: Class issue.

 
0
  #6
Oct 28th, 2008
I'll write up a better explination later, but here is a class that would create the widget and check your answers:
  1. from Tkinter import *
  2.  
  3. class question():
  4.  
  5. def __init__(self, parent, q_text, a_list, correct, incorrect):
  6. self.frame = Frame(parent)
  7. self.q_text = q_text
  8. self.a_list = a_list
  9. self.correct = correct
  10. self.incorrect = incorrect
  11.  
  12. Label(self.frame, text=self.q_text).grid(row=0, column=0)
  13. self.entry = Entry(self.frame)
  14. self.label = Label(self.frame)
  15.  
  16. self.entry.grid(row=1, column=0)
  17. self.label.grid(row=1, column=1)
  18. Button(self.frame, text='check', command=self.check).grid(row=2, column=1)
  19. def grid(self, y, x, yspan=1, xspan=1):
  20. self.frame.grid(row=y, column=x, rowspan=yspan, columnspan=xspan)
  21.  
  22. def pack(self):
  23. self.frame.pack()
  24.  
  25. def check(self):
  26. self.answer = self.entry.get().lower()
  27. for x in self.a_list:
  28. if self.answer.find(x)!=-1:
  29. self.label['text'] = self.correct
  30. return 1
  31. self.label['text'] = self.incorrect
  32. return 0
  33.  
  34. root = Tk()
  35. x = question(root, 'Which Character is this:', ['samus', 'metriod', 'prime'], "That's Correct!", "That's Incorrect!")
  36. x.grid(0, 0)
  37. root.mainloop()

you can see the call to the class there at the end.
Last edited by tyincali; Oct 28th, 2008 at 4:23 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC