GUI Output Text

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wocosc
    New Member
    • Mar 2007
    • 18

    #1

    GUI Output Text

    I have my gui set up, and I am using a loop to set up output text to be displayed in the gui if the conditions are met. However, what handler do i use to display the text in the gui?
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by wocosc
    I have my gui set up, and I am using a loop to set up output text to be displayed in the gui if the conditions are met. However, what handler do i use to display the text in the gui?
    You'll have to remind me: Tkinter or wxPython?

    I think in Tk it's
    Code:
    self.text.insert(text)
    where self.text is a Tk.Text widget.
    In wx it's
    Code:
    self.myTextCtrl.AppendText(text)
    where myTextCtrl is a multiline wx.TextCtrl.

    Comment

    • wocosc
      New Member
      • Mar 2007
      • 18

      #3
      Originally posted by bartonc
      You'll have to remind me: Tkinter or wxPython?

      I think in Tk it's
      Code:
      self.text.insert(text)
      where self.text is a Tk.Text widget.
      In wx it's
      Code:
      self.myTextCtrl.AppendText(text)
      where myTextCtrl is a multiline wx.TextCtrl.

      I am really just using Python v2.5 . I am not supposed to use other gui's. Really, we are just using the graphics .py file to make our gui.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by wocosc
        I am really just using Python v2.5 . I am not supposed to use other gui's. Really, we are just using the graphics .py file to make our gui.
        The only GUI that "comes with" python is Tkinter. Period. Tkinter has some graphics capability. Graphics packages generally don't make GUIs; GUIs generally can handle graphics.

        Please show some work that you have done and we will get this straightened out.

        Comment

        • wocosc
          New Member
          • Mar 2007
          • 18

          #5
          Here is the code. The Cases (1,2,3,4) has the text that needs to be displayed if the certain condition is met.

          Code:
          from graphics import *
          
          def main():
          	win = GraphWin("BMI Calculator", 1200,700)
          	win.setCoords(0.0, 0.0, 3.0, 4.0)
          
          	## Draw the interface
          	Text(Point(1,3), " Height (inches):").draw(win)
          	Text(Point(1,3.5), "Weight (lbs):").draw(win)
          	Text(Point(1,1), "Body Mass Index:").draw(win)
          	ht = Entry(Point(2,3), 5)
          	ht.setText("0.0")
          	ht.draw(win)
          	wt = Entry(Point(2,3.5), 5)
          	wt.setText("0.0")
          	wt.draw(win)
          
          	output = Text(Point(2,1),"")
          	output.draw(win)
          	suggestion = Text(Point(1,1),"")
          	suggestion.draw(win)
          	button = Text(Point(1.5,2.0),"Convert It")
          	button.draw(win)
          	Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)
          
          	## Wait for a mouse click
          	win.getMouse()
          
          	## Convert input
          	ht = eval(ht.getText())
          	wt = eval(wt.getText())
          	bmi = wt * 703.0 / (ht * ht)
          
          	## Display output and change button
          	output.setText("%0.1f" % bmi)
          	button.setText("Quit")
          
          	## Wait for click and then quit
          	win.getMouse()
          	win.close()
          
          	## Case one
          	n = 8 - bmi
          	new = (18 * (ht * ht)) / 703.3
          	difference = new - wt  
          	x = difference * 2
          	if bmi < 16:
          	## figure out how to display the following text..
          		suggestion.setText("%0.1f","I am ordering a work-up for eating disorders to be on the safe side. In the mean time,I would like you to gain", n  ,"pounds over the next", x," weeks").draw(win)
          
          	## Case 2.
          	elif bmi < 25:
          	## figure out how to display the following text..
          		output.setText("%0.1f", "Keep up the good work!"
          
          	## Case 3.
          	elif bmi =< 30:
          	## figure out how to display the following text..
          		display the message "You could stand to lose weight. I'd like you to lose", n," pounds over the next", x," months."
          
          	## Case 4.
          	elif bmi > 30:
          	## figure out how to display the following text..
          		display the message "We have a problem here. You need to lose", n," pounds over the next," x," months."

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Thanks for posting the code.
            I found the documentation here, but haven't found the download yet, so I can't play with it yet.

            Anybody else ever heard of graphics.py (a thin wrapper for Tkinter) before?

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by bartonc
              Thanks for posting the code.
              I found the documentation here, but haven't found the download yet, so I can't play with it yet.

              Anybody else ever heard of graphics.py (a thin wrapper for Tkinter) before?
              The Text object methode that you are looking for is:
              setText(string)
              Sets the text of the object to string.

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by bartonc
                Thanks for posting the code.
                I found the documentation here, but haven't found the download yet, so I can't play with it yet.

                Anybody else ever heard of graphics.py (a thin wrapper for Tkinter) before?
                It turns out that Wartburg College has all the links for graphics.py.

                Comment

                • wocosc
                  New Member
                  • Mar 2007
                  • 18

                  #9
                  Originally posted by bartonc
                  The Text object methode that you are looking for is:
                  setText(string)
                  Sets the text of the object to string.
                  so i put the text i want to display in the (string)?

                  Comment

                  • bartonc
                    Recognized Expert Expert
                    • Sep 2006
                    • 6478

                    #10
                    Originally posted by wocosc
                    so i put the text i want to display in the (string)?
                    Yet. That's correct. Just try it and see what happens. Post back with code and/or error messages. We'll get you going.

                    BTW, are you taking a class at Wartburg?

                    Comment

                    • wocosc
                      New Member
                      • Mar 2007
                      • 18

                      #11
                      Code:
                      from graphics import *
                      
                      def main():
                          win = GraphWin("BMI Calculator", 1200,700)
                          win.setCoords(0.0, 0.0, 3.0, 4.0)
                      
                          ## Draw the interface
                          Text(Point(1,3), " Height (inches):").draw(win)
                          Text(Point(1,3.5), "Weight (lbs):").draw(win)
                          Text(Point(1,1), "Body Mass Index:").draw(win)
                          ht = Entry(Point(2,3), 5)
                          ht.setText("0.0")
                          ht.draw(win)
                          wt = Entry(Point(2,3.5), 5)
                          wt.setText("0.0")
                          wt.draw(win)
                      
                          output = Text(Point(2,1),"")
                          output.draw(win)
                          suggestion = Text(Point(1,1),"")
                          suggestion.draw(win)
                          button = Text(Point(1.5,2.0),"Convert It")
                          button.draw(win)
                          Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)
                      
                          ## Wait for a mouse click
                          win.getMouse()
                      
                          ## Convert input
                          ht = eval(ht.getText())
                          wt = eval(wt.getText())
                          bmi = wt * 703.0 / (ht * ht)
                      
                          ## Display output and change button
                          output.setText("%0.1f" % bmi)
                          button.setText("Quit")
                      
                          ## Wait for click and then quit
                          win.getMouse()
                          win.close()
                      
                          ## Case one
                          n = 8 - bmi
                          new = (18 * (ht * ht)) / 703.3
                          difference = new - wt  
                          x = difference * 2
                          if bmi < 16:
                          ## figure out how to display the following text..
                              1 = "I am ordering a work-up for eating disorders to be on the safe side. In the mean time,I would like you to gain", n  ,"pounds over the next", x," weeks").draw(win)
                              setText(1)
                          ## Case 2.
                          if bmi < 25:
                          ## figure out how to display the following text..
                              output.setText("%0.1f", "Keep up the good work!"
                      
                          ## Case 3.
                          if bmi =< 30:
                          ## figure out how to display the following text..
                              display the message "You could stand to lose weight. I'd like you to lose", n," pounds over the next", x," months."
                      
                          ## Case 4.
                          if bmi > 30:
                          ## figure out how to display the following text..
                              display the message "We have a problem here. You need to lose", n," pounds over the next," x," months."


                      did I set case 1 up right?/??????
                      Last edited by bartonc; Mar 28 '07, 04:07 AM. Reason: added [code][/code] tags

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #12
                        Here are a couple of things that I see:
                        First: You need to use CODE tags! I'm the only one in this forum who can add them after you post, so nobody who is trying to get an answer for you can see the structure (or lack of it in this case) of your code.
                        Code:
                            ## Case one
                            n = 8 - bmi
                            new = (18 * (ht * ht)) / 703.3
                            difference = new - wt  
                            x = difference * 2
                            if bmi < 16:
                            ## figure out how to display the following text..
                                1 = "I am ordering a work-up for eating disorders to be on the safe side. In the mean time,I would like you to gain", n  ,"pounds over the next", x," weeks")
                        Assign to a variable only and use string formats as before, as in:
                        message = "I am ordering a work-up for eating disorders to be on the safe side. In the mean time,I would like you to gain %d pounds over the next %d weeks" %(n, x)
                        Then, after all the cases have run and decided what the value of message is:
                        Code:
                        output.setText(message)
                        The proper syntax for "less than or equal" is <= not:
                        Code:
                               ## Case 3.
                            if bmi =< 30:

                        Comment

                        • bartonc
                          Recognized Expert Expert
                          • Sep 2006
                          • 6478

                          #13
                          Originally posted by bartonc
                          Here are a couple of things that I see:
                          First: You need to use CODE tags! I'm the only one in this forum who can add them after you post, so nobody who is trying to get an answer for you can see the structure (or lack of it in this case) of your code.
                          Code:
                              ## Case one
                              n = 8 - bmi
                              new = (18 * (ht * ht)) / 703.3
                              difference = new - wt  
                              x = difference * 2
                              if bmi < 16:
                              ## figure out how to display the following text..
                                  1 = "I am ordering a work-up for eating disorders to be on the safe side. In the mean time,I would like you to gain", n  ,"pounds over the next", x," weeks")
                          Assign to a variable only and use string formats as before, as in:
                          message = "I am ordering a work-up for eating disorders to be on the safe side. In the mean time,I would like you to gain %d pounds over the next %d weeks" %(n, x)
                          Then, after all the cases have run and decided what the value of message is:
                          Code:
                          output.setText(message)
                          The proper syntax for "less than or equal" is <= not:
                          Code:
                                 ## Case 3.
                              if bmi =< 30:
                          Code:
                                  output.setText("%0.1f", "Keep up the good work!"
                          becomes
                          Code:
                          message = "%0.1f, Keep up the good work!" % bmi

                          Comment

                          • bartonc
                            Recognized Expert Expert
                            • Sep 2006
                            • 6478

                            #14
                            Originally posted by bartonc
                            Code:
                                    output.setText("%0.1f", "Keep up the good work!"
                            becomes
                            Code:
                            message = "%0.1f, Keep up the good work!" % bmi
                            Cases are written like this:
                            Code:
                            case = 1
                            if case == 1:
                                print "one"
                            elif case == 2:
                                print "two"
                            else:
                                print "not a valid case"

                            Comment

                            Working...