I've just taken a beginners project from Vegaseat and modified it to fit with a Tkinter tutorial.

I'm just trying to get a Tk window that has a "Go!" button and by pressing it prints the sentence.

I'm having lots of trouble. I've gained a lot from this though, so thanks mounds to Vegaseat

# sentence generator 

import random
from Tkinter import *

# Tkinter is used to make the button
class App:
	def __init__(self, master):
		frame = Frame(master)
		frame.pack()
		
		sentence.button = Button(frame, text="Go!", command=sentence.makeSentence)

	def makeSentence(part1, part2, part3, n=1):
		"""return n random sentences"""
		#convert to lists
		p1 = part1.split('\n')
		p2 = part2.split('\n')
		p3 = part3.split('\n')
		#shuffle the lists
		random.shuffle(p1)
		random.shuffle(p2)
		random.shuffle(p3)
		#concatinate the sentences
		sentence = []
		for k in range(n):
			try:
				s = p1[k] + ' ' + p2[k] + ' ' + p3[k]
				s = s.capitalize() + '.'
				sentence.append(s)
			except IndexError:
				 break
		return sentence
	
# break a typical sentence into 3 parts
# first part of a sentence (subject)
part1 = """\
A panda
Gary Busey"""

# (action)
part2 = """\
slaps
eats
gores
"""

# (object)
part3 = """\
puppies
bananas
"""

sentence = makeSentence(part1, part2, part3)
for item in sentence:
	print item
	
root = Tk()

app = App(root)

root.mainloop()

Anyone have a fix for this code? judging by my errors that the Terminal window show upon running I have problems with the <module> sentence = makeSentence(part1, part2, part3) towards the end. Thanks in advance

Recommended Answers

All 7 Replies

I attach a slightly corrected program

Definitely better, but I'm still getting the same undefined error.

Strange, it works very well here. Could you post the full Exception traceback ?

File "sentenceGenerator3.py", line 122, in <module>
sentence = makeSentence(part1, part2, part3)
NameError: name 'makeSentence' is not defined

makeSentence is a function of the class App. It should be called as such App.makeSentence

The file I attached has only 68 lines. No error can occur on line 122 ! And I commented out the line sentence = ... . So you must have done something to my file. Try to reload it :)

#sentence = makeSentence(part1, part2, part3)
#for item in sentence:
#		print item

That is what I got wrong. Didn't know about the pound signs and that they go there... Thanks guys

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.