Why it does not work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • foggylake
    New Member
    • Feb 2012
    • 5

    Why it does not work?

    Dear friends,
    I took this code from the Python and Tkinter Programming book by JOHN E. GRAYSON. A calculator example from there.
    Why it does not work?
    Yours Foggylake
    Attached Files
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I'll bet you received a syntax error. The first three lines are not valid code. Remove the first two lines and modify the third line to look like:
    Code:
    from Tkinter import *
    In addition, your indentation is wrong and must be corrected in order to work.

    Comment

    • foggylake
      New Member
      • Feb 2012
      • 5

      #3
      Sorry, dear bvdet
      , but what do you mean saying of "my indentation"? And are you joking about cleaning def frame(root, side):
      w = Frame(root)? Please,be kind and correct the code in the file for to work and put the last one, please.
      I am a pupil.
      yours

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        I was not joking. In the file you posted, the first three lines are:
        Code:
        Python 3.2.2 (default, Sep  4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
        Type "copyright", "credits" or "license()" for more information.
        >>> from Tkinter import *
        They are not valid code and will produce a syntax error.

        Python uses whitespace indentation to delimit blocks of code. The indentation of the code you posted appears to be incorrect. For example, in the class definition Calculator, you are creating frames and buttons, but there is no indentation whatsoever (for key in and for char in). Maybe you should go back to where you copied the code and see what the indentation is supposed to look like. Before that, maybe you should learn more about Python indentation. Python: Myths about Indentation should help.

        Comment

        • foggylake
          New Member
          • Feb 2012
          • 5

          #5
          Dear bvdet,
          many thanks for the link and I got of the first two lines/
          Could you repair that file for to work, because the code was written in the book by Guru and I can not understand where is mistake.
          Seeing the result of your repairing can be better for me to learn.
          Test it working, please.
          I am a pupil.
          yours

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Don't use being a pupil as an excuse. There is a lot of code posted on the internet that you could study to better understand indentation.

            I found a GUI calculator script I saved two years ago that I got off the internet (author unknown). Instead of trying to fix your indentation errors, I'll post it here. As luck would have it, it's the same code as yours!
            Code:
            from Tkinter import *
             
            def frame(root, side): 
                w = Frame(root)
                w.pack(side=side, expand=YES, fill=BOTH)
                return w
             
            def button(root, side, text, command=None): 
                w = Button(root, text=text, command=command) 
                w.pack(side=side, expand=YES, fill=BOTH)
                return w
             
            class Calculator(Frame):
                def __init__(self):
                    Frame.__init__(self)
                    self.option_add('*Font', 'Verdana 12 bold')
                    self.pack(expand=YES, fill=BOTH)
                    self.master.title('Simple Calculator')
                    self.master.iconname("calc1")
             
                    display = StringVar()
                    displayEntry = Entry(self, relief=SUNKEN, textvariable=display)
                    displayEntry.pack(side=TOP, expand=YES, fill=BOTH)
             
                    for key in ("123", "456", "789", "-0."):
                        keyF = frame(self, TOP)
                        for char in key:
                            button(keyF, LEFT, char,
                                   lambda w=display, c=char: w.set(w.get() + c))
             
                    opsF = frame(self, TOP)
                    for char in "+-*/=":
                        if char == '=':
                            btn = button(opsF, LEFT, char)
                            btn.bind('<ButtonRelease-1>',
                                     lambda e, s=self, w=display: s.calc(w), '+')
                        else:
                            btn = button(opsF, LEFT, char,
                               lambda w=display, s=' %s '%char: w.set(w.get()+s))
             
                    clearF = frame(self, BOTTOM)
                    button(clearF, LEFT, 'Clr', lambda w=display: w.set(''))
             
                def calc(self, display):
                    try:
                        display.set(str(eval(display.get())))
                    except:
                        display.set("ERROR")
             
            if __name__ == '__main__':
                Calculator().mainloop()

            Comment

            • foggylake
              New Member
              • Feb 2012
              • 5

              #7
              Dear bvdet,
              many thanks for you to worry, but F5 and ImportError: No module named Tkinter ... :)
              yours

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                If you are in Python 3, that would be:
                import tkinter

                Comment

                • foggylake
                  New Member
                  • Feb 2012
                  • 5

                  #9
                  Dear bvdet,
                  thank you very much for your help!
                  yours

                  Comment

                  Working...