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?

Recommended Answers

All 5 Replies

I don't see a class in your code.

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.

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?

When in doubt run a simplified test code:

# test Tkinter Entry

import Tkinter as tk

def question5(event):
    if entry1.get().lower() in ["samus", "metroid", "prime", "metroid prime"]:
        label2['text'] = "Its the amazing Metroid game heroine Samus, good call!"
    else:
        label2['text'] = "Sigh.. try again."
            
root = tk.Tk()

# first entry with label
label1 = tk.Label(root, text='Enter heroine:')
label1.grid(row=0, column=0)
entry1 = tk.Entry(root, bg='yellow', width=50)
entry1.grid(row=1, column=0, padx=5)
label2 = tk.Label(root, text=' ', fg='red')
label2.grid(row=2, column=0, pady=5)

# start cursor in enter1
entry1.focus()
# value has been entered in enter1 now press return key to check content
entry1.bind('<Return>', question5)

root.mainloop()

I'll write up a better explination later, but here is a class that would create the widget and check your answers:

from Tkinter import *

class question():

	def __init__(self, parent, q_text, a_list, correct, incorrect):
		self.frame = Frame(parent)
		self.q_text = q_text
		self.a_list = a_list
		self.correct = correct
		self.incorrect = incorrect
		
		Label(self.frame, text=self.q_text).grid(row=0, column=0)
		self.entry = Entry(self.frame)
		self.label = Label(self.frame)
		
		self.entry.grid(row=1, column=0)
		self.label.grid(row=1, column=1)
		Button(self.frame, text='check', command=self.check).grid(row=2, column=1)
	def grid(self, y, x, yspan=1, xspan=1):
		self.frame.grid(row=y, column=x, rowspan=yspan, columnspan=xspan)
	
	def pack(self):
		self.frame.pack()
	
	def check(self):
		self.answer = self.entry.get().lower()
		for x in self.a_list:
			if self.answer.find(x)!=-1:
				self.label['text'] = self.correct
				return 1
			self.label['text'] = self.incorrect
			return 0
	
root = Tk()
x = question(root, 'Which Character is this:', ['samus', 'metriod', 'prime'], "That's Correct!", "That's Incorrect!")
x.grid(0, 0)
root.mainloop()

you can see the call to the class there at the end.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.