I have a sentence generator that I set up with a "make" button to print the generated sentence, but I want it to print to the resultBox area, and instead it prints to the terminal window. I'm assuming it has something to do with the return sentence portion of it. But any help would be appreciated

class MainFrame(wx.Frame):

	def __init__(self):
		wx.Frame.__init__(self, None, title = 'Sentence Generator',
				pos = (200, 75), size = (WINDOW_WIDTH, WINDOW_HEIGHT))			
		
		self.background = wx.Panel(self)

		self.makeBtn = wx.Button(self.background, label = 'Make') 
		self.makeBtn.Bind(wx.EVT_BUTTON, self.displaySentence)
		
		self.resultBox = wx.TextCtrl(self.background, style = wx.TE_READONLY | wx.TE_MULTILINE)
		
		self.hBox = wx.BoxSizer()
		self.hBox.Add(self.resultBox, proportion = 1, border = 0)
		
		self.hBox.Add(self.makeBtn, proportion = 0, border = 0)
		
		self.vBox = wx.BoxSizer(wx.VERTICAL)
		self.vBox.Add(self.hBox, proportion = 0, flag = wx.EXPAND, border = 0)
		self.vBox.Add(self.result, proportion = 1, flag = wx.EXPAND, border = 0)
		
		self.background.SetSizer(self.vBox)
		self.Show()

	def displaySentence(self, event):
		for item in self.makeSentence(part1, part2, part3):
			print item
			
	def resultBox(self, event):
		self.resultBox.SetValue(self.makeSentence.GetValue())

	def makeSentence(self, 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

This should eventually work:

class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title = 'Sentence Generator',
                pos = (200, 75), size = (WINDOW_WIDTH, WINDOW_HEIGHT))			
        
        self.background = wx.Panel(self)

        self.makeBtn = wx.Button(self.background, label = 'Make') 
        self.makeBtn.Bind(wx.EVT_BUTTON, self.displaySentence)
        
        self.resultBox = wx.TextCtrl(self.background, style = wx.TE_READONLY | wx.TE_MULTILINE)
        
        self.hBox = wx.BoxSizer()
        self.hBox.Add(self.resultBox, proportion = 1, border = 0)
        
        self.hBox.Add(self.makeBtn, proportion = 0, border = 0)
        
        self.vBox = wx.BoxSizer(wx.VERTICAL)
        self.vBox.Add(self.hBox, proportion = 0, flag = wx.EXPAND, border = 0)
        self.vBox.Add(self.result, proportion = 1, flag = wx.EXPAND, border = 0)
        
        self.background.SetSizer(self.vBox)
        self.Show()

    def displaySentence(self, event):
        # looks like result is a list of sentences
        result = self.makeSentence(part1, part2, part3)
        # join the list to a string of sentences
        s = "".join(result)
        # this needs a string
        self.resultBox.SetValue(s)
            
    def makeSentence(self, 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() + '.' + '\n'
                sentence.append(s)
            except IndexError:
                 break
        # a list of sentences
        return sentence

That is perfect. Thank you.

I was also trying to set the make button up to be in the middle of the top panel, but the pos commands weren't effecting it. I was also under the impression that wx.TextCtrl logged the data transferred to it. Shouldn't when I make more than one sentence, the first ones scroll down below the newer ones? Thanks kindly.

This article has been dead for over six months. Start a new discussion instead.