Tkinter, a lot of buttons, make prog shorter?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • skanemupp@yahoo.se

    Tkinter, a lot of buttons, make prog shorter?

    is there anyway to make this shorter? i hate having these big blocks
    of similar-looking code, very unaesthetic.
    maybe doesnt matter good-code-wise?
    anyway can i make some function that makes this shorter?
    like put the etiquettes on the button froma string of
    '123+456-789*0Cr/' ?
    problem is the command and lambda-func for each button is different.


    self.btnDisplay = Button(self,tex t='1',command=l ambda
    n="1":self.Disp lay(n),width=2, height=2)
    self.btnDisplay .grid(row=3, column=0)

    self.btnDisplay = Button(self,tex t='2',command=l ambda
    n="2":self.Disp lay(n),width=2, height=2)
    self.btnDisplay .grid(row=3, column=1)

    self.btnDisplay = Button(self,tex t='3',command=l ambda
    n="3":self.Disp lay(n),width=2, height=2)
    self.btnDisplay .grid(row=3, column=2)

    self.btnDisplay = Button(self,tex t='+',command=l ambda
    n="+":self.Disp lay(n),width=2, height=2)
    self.btnDisplay .grid(row=3, column=3)

    self.btnDisplay = Button(self,tex t='4',command=l ambda
    n="4":self.Disp lay(n),width=2, height=2)
    self.btnDisplay .grid(row=4, column=0)

    self.btnDisplay = Button(self,tex t='5',command=l ambda
    n="5":self.Disp lay(n),width=2, height=2)
    self.btnDisplay .grid(row=4, column=1)

    self.btnDisplay = Button(self,tex t='6',command=l ambda
    n="6":self.Disp lay(n),width=2, height=2)
    self.btnDisplay .grid(row=4, column=2)

    self.btnDisplay = Button(self,tex t='-',command=lambd a
    n="-":self.Display( n),width=2,heig ht=2)
    self.btnDisplay .grid(row=4, column=3)

    self.btnDisplay = Button(self,tex t='7',command=l ambda
    n="7":self.Disp lay(n),width=2, height=2)
    self.btnDisplay .grid(row=5, column=0)

    self.btnDisplay = Button(self,tex t='8',command=l ambda
    n="8":self.Disp lay(n),width=2, height=2)
    self.btnDisplay .grid(row=5, column=1)

    self.btnDisplay = Button(self,tex t='9',command=l ambda
    n="9":self.Disp lay(n),width=2, height=2)
    self.btnDisplay .grid(row=5, column=2)

    self.btnDisplay = Button(self,tex t='*',command=l ambda
    n="*":self.Disp lay(n),width=2, height=2)
    self.btnDisplay .grid(row=5, column=3)

    self.btnDisplay = Button(self,tex t='0',command=l ambda
    n="0":self.Disp lay(n),width=2, height=2)
    self.btnDisplay .grid(row=6, column=0)

    self.btnDisplay =
    Button(self,tex t='C',command=s elf.Clean,width =2,height=2)
    self.btnDisplay .grid(row=6, column=1)

    self.btnDisplay = Button(self,tex t='r',command=l ambda
    n="r":self.Disp lay(n),width=2, height=2)
    self.btnDisplay .grid(row=6, column=2)

    self.btnDisplay = Button(self,tex t='/',command=lambd a
    n="/":self.Display( n),width=2,heig ht=2)
    self.btnDisplay .grid(row=6, column=3)
  • Terry Reedy

    #2
    Re: Tkinter, a lot of buttons, make prog shorter?


    <skanemupp@yaho o.sewrote in message
    news:c9bf3bb9-71e4-48f6-aae0-fa9400ca8155@j1 g2000prb.google groups.com...
    | is there anyway to make this shorter? i hate having these big blocks
    | of similar-looking code, very unaesthetic.
    | maybe doesnt matter good-code-wise?
    | anyway can i make some function that makes this shorter?
    | like put the etiquettes on the button froma string of
    | '123+456-789*0Cr/' ?
    | problem is the command and lambda-func for each button is different.
    |
    |
    | self.btnDisplay = Button(self,tex t='1',command=l ambda
    | n="1":self.Disp lay(n),width=2, height=2)
    | self.btnDisplay .grid(row=3, column=0)
    |
    | self.btnDisplay = Button(self,tex t='2',command=l ambda
    | n="2":self.Disp lay(n),width=2, height=2)
    | self.btnDisplay .grid(row=3, column=1)
    |
    | self.btnDisplay = Button(self,tex t='3',command=l ambda
    | n="3":self.Disp lay(n),width=2, height=2)
    | self.btnDisplay .grid(row=3, column=2)
    |
    | self.btnDisplay = Button(self,tex t='+',command=l ambda
    | n="+":self.Disp lay(n),width=2, height=2)
    | self.btnDisplay .grid(row=3, column=3)
    |
    | self.btnDisplay = Button(self,tex t='4',command=l ambda
    | n="4":self.Disp lay(n),width=2, height=2)
    | self.btnDisplay .grid(row=4, column=0)
    |
    | self.btnDisplay = Button(self,tex t='5',command=l ambda
    | n="5":self.Disp lay(n),width=2, height=2)
    | self.btnDisplay .grid(row=4, column=1)
    |
    | self.btnDisplay = Button(self,tex t='6',command=l ambda
    | n="6":self.Disp lay(n),width=2, height=2)
    | self.btnDisplay .grid(row=4, column=2)
    |
    | self.btnDisplay = Button(self,tex t='-',command=lambd a
    | n="-":self.Display( n),width=2,heig ht=2)
    | self.btnDisplay .grid(row=4, column=3)
    |
    | self.btnDisplay = Button(self,tex t='7',command=l ambda
    | n="7":self.Disp lay(n),width=2, height=2)
    | self.btnDisplay .grid(row=5, column=0)
    |
    | self.btnDisplay = Button(self,tex t='8',command=l ambda
    | n="8":self.Disp lay(n),width=2, height=2)
    | self.btnDisplay .grid(row=5, column=1)
    |
    | self.btnDisplay = Button(self,tex t='9',command=l ambda
    | n="9":self.Disp lay(n),width=2, height=2)
    | self.btnDisplay .grid(row=5, column=2)
    |
    | self.btnDisplay = Button(self,tex t='*',command=l ambda
    | n="*":self.Disp lay(n),width=2, height=2)
    | self.btnDisplay .grid(row=5, column=3)
    |
    | self.btnDisplay = Button(self,tex t='0',command=l ambda
    | n="0":self.Disp lay(n),width=2, height=2)
    | self.btnDisplay .grid(row=6, column=0)
    |
    | self.btnDisplay =
    | Button(self,tex t='C',command=s elf.Clean,width =2,height=2)
    | self.btnDisplay .grid(row=6, column=1)
    |
    | self.btnDisplay = Button(self,tex t='r',command=l ambda
    | n="r":self.Disp lay(n),width=2, height=2)
    | self.btnDisplay .grid(row=6, column=2)
    |
    | self.btnDisplay = Button(self,tex t='/',command=lambd a
    | n="/":self.Display( n),width=2,heig ht=2)
    | self.btnDisplay .grid(row=6, column=3)

    With the exception of the 'C' button, the only thing different is the label
    and position.
    I believe (untested, obviously)

    def btn(self, txt, r, c):
    self.btnDisplay = Button(self, text=txt, command=lambda:
    self.Display(tx t), width=2,height= 2)
    self.btnDisplay .grid(row=r, column=r)

    will work.

    tjr






    Comment

    • Peter Otten

      #3
      Re: Tkinter, a lot of buttons, make prog shorter?

      skanemupp@yahoo .se wrote:
      is there anyway to make this shorter? i hate having these big blocks
      of similar-looking code, very unaesthetic.
      maybe doesnt matter good-code-wise?
      anyway can i make some function that makes this shorter?
      like put the etiquettes on the button froma string of
      '123+456-789*0Cr/' ?
      problem is the command and lambda-func for each button is different.
      You can look up the command in a dictionary:

      commands = {"C": self.Clean}
      top = 3
      for i, text in enumerate("123+ 456-789*0Cr/"):
      row, column = divmod(i, 4)
      try:
      command = commands[text]
      except KeyError:
      def command(n=text) :
      self.Display(n)
      button = Button(self, text=text, command=command , width=2, height=2)
      button.grid(row =top+row, column=column)

      The problem with this is that the shorter code may take a bit longer to
      understand, so that there is no net win for the reader. A compromise would be
      to define a helper function:

      def make_button(sel f, row, column, text, command):
      # implementation left as an exercise

      # use it:
      self.make_butto n(3, 0, "1", lambda n="1": ...)
      self.make_butto n(3, 1, "2", lambda n="2": ...)
      ....

      Peter

      Comment

      • 7stud

        #4
        Re: Tkinter, a lot of buttons, make prog shorter?

        On Apr 6, 1:59 pm, skanem...@yahoo .se wrote:
        is there anyway to make this shorter? i hate having these big blocks
        of similar-looking code, very unaesthetic.
        maybe doesnt matter good-code-wise?
        anyway can i make some function that makes this shorter?
        like put the etiquettes on the button froma string of
        '123+456-789*0Cr/' ?
        problem is the command and lambda-func for each button is different.
        >
                self.btnDisplay = Button(self,tex t='1',command=l ambda
        n="1":self.Disp lay(n),width=2, height=2)
                self.btnDisplay .grid(row=3, column=0)
        >
                self.btnDisplay = Button(self,tex t='2',command=l ambda
        n="2":self.Disp lay(n),width=2, height=2)
                self.btnDisplay .grid(row=3, column=1)
        >
                self.btnDisplay = Button(self,tex t='3',command=l ambda
        n="3":self.Disp lay(n),width=2, height=2)
                self.btnDisplay .grid(row=3, column=2)
        >
                self.btnDisplay = Button(self,tex t='+',command=l ambda
        n="+":self.Disp lay(n),width=2, height=2)
                self.btnDisplay .grid(row=3, column=3)
        >
                self.btnDisplay = Button(self,tex t='4',command=l ambda
        n="4":self.Disp lay(n),width=2, height=2)
                self.btnDisplay .grid(row=4, column=0)
        >
                self.btnDisplay = Button(self,tex t='5',command=l ambda
        n="5":self.Disp lay(n),width=2, height=2)
                self.btnDisplay .grid(row=4, column=1)
        >
                self.btnDisplay = Button(self,tex t='6',command=l ambda
        n="6":self.Disp lay(n),width=2, height=2)
                self.btnDisplay .grid(row=4, column=2)
        >
                self.btnDisplay = Button(self,tex t='-',command=lambd a
        n="-":self.Display( n),width=2,heig ht=2)
                self.btnDisplay .grid(row=4, column=3)
        >
                self.btnDisplay = Button(self,tex t='7',command=l ambda
        n="7":self.Disp lay(n),width=2, height=2)
                self.btnDisplay .grid(row=5, column=0)
        >
                self.btnDisplay = Button(self,tex t='8',command=l ambda
        n="8":self.Disp lay(n),width=2, height=2)
                self.btnDisplay .grid(row=5, column=1)
        >
                self.btnDisplay = Button(self,tex t='9',command=l ambda
        n="9":self.Disp lay(n),width=2, height=2)
                self.btnDisplay .grid(row=5, column=2)
        >
                self.btnDisplay = Button(self,tex t='*',command=l ambda
        n="*":self.Disp lay(n),width=2, height=2)
                self.btnDisplay .grid(row=5, column=3)
        >
                self.btnDisplay = Button(self,tex t='0',command=l ambda
        n="0":self.Disp lay(n),width=2, height=2)
                self.btnDisplay .grid(row=6, column=0)
        >
                self.btnDisplay =
        Button(self,tex t='C',command=s elf.Clean,width =2,height=2)
                self.btnDisplay .grid(row=6, column=1)
        >
                self.btnDisplay = Button(self,tex t='r',command=l ambda
        n="r":self.Disp lay(n),width=2, height=2)
                self.btnDisplay .grid(row=6, column=2)
        >
                self.btnDisplay = Button(self,tex t='/',command=lambd a
        n="/":self.Display( n),width=2,heig ht=2)
                self.btnDisplay .grid(row=6, column=3)
        You said you were an experienced programmer, but you keep posting code
        with the same mistakes over and over again. Why are you attaching the
        buttons to self?

        Comment

        Working...