TK-grid problem, please help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ray

    #1

    TK-grid problem, please help

    hi, I have a question about how to use .grid_forget (in python/TK)

    I need to work on grid repeatly. everytime when a button is pressed,
    the rows of grid is different. such like, first time, it generate 10
    rows of data.
    2nd time, it maybe only 5 rows. so I need a way to RESET the grid data
    every time. how can I do it? by grid_forger()?, then would anyone can
    help on
    how to use grid_forget()
    the sample code as following:

    #####begin of program######## #######

    from Tkinter import *
    def mygrid(text):
    ######## how to use grid_forget() to clean the grid??######### ##
    rows = []
    count=int(text)
    for i in range(count):
    cols = []
    for j in range(4):
    e = Entry(frame3, relief=RIDGE)
    e.grid(row=i, column=j, sticky=NSEW)
    e.insert(END, '%d.%d' % (i, j))
    cols.append(e)
    rows.append(col s)


    root=Tk()

    frame1=Frame(ro ot, width=150, height=100)
    frame1.pack()

    text=Entry(fram e1)
    text.pack(side= LEFT)

    button=Button(f rame1, text='generate grid', command=(lambda :
    mygrid(text.get ())))

    button.pack()

    frame2=Frame(ro ot, width=150, height=100)
    frame2.pack()

    button2=Button( frame2, text='exit', command=root.qu it)
    button2.pack()

    frame3=Frame(ro ot, width=150, height=300)
    frame3.pack()

    root.mainloop()

    #####end of program######## #######
  • Anton Vredegoor

    #2
    Re: TK-grid problem, please help

    Ray wrote:
    hi, I have a question about how to use .grid_forget (in python/TK)
    >
    I need to work on grid repeatly. everytime when a button is pressed,
    the rows of grid is different. such like, first time, it generate 10
    rows of data.
    2nd time, it maybe only 5 rows. so I need a way to RESET the grid data
    every time. how can I do it? by grid_forger()?, then would anyone can
    help on
    how to use grid_forget()
    the sample code as following:
    I'm not sure if it solves your problem but this modification of your
    code at least *looks* like it works better. The entries are completely
    destroyed so that the next time you call the function they can be
    recreated.

    The trick I am using is to use a list in the arguments of the function
    but that is a bit of a hack, the list 'remembers' its state from the
    last time the function was called, I think one should use classes for
    bookkeeping such things instead.

    from Tkinter import *

    def mygrid(text,M = []):
    ######## how to use grid_forget() to clean the grid??######### ##
    while M:
    x = M.pop()
    x.destroy()
    rows = []
    count=int(text)
    for i in range(count):
    cols = []
    for j in range(4):
    e = Entry(frame3, relief=RIDGE)
    M.append(e)
    e.grid(row=i, column=j, sticky=NSEW)
    e.insert(END, '%d.%d' % (i, j))
    cols.append(e)
    rows.append(col s)


    root=Tk()

    frame1=Frame(ro ot, width=150, height=100)
    frame1.pack()

    text=Entry(fram e1)
    text.pack(side= LEFT)

    button=Button(f rame1, text='generate grid', command=(lambda :
    mygrid(text.get ())))

    button.pack()

    frame2=Frame(ro ot, width=150, height=100)
    frame2.pack()

    button2=Button( frame2, text='exit', command=root.qu it)
    button2.pack()

    frame3=Frame(ro ot, width=150, height=300)
    frame3.pack()

    root.mainloop()

    A.

    Comment

    • James Stroud

      #3
      Re: TK-grid problem, please help

      Ray wrote:
      hi, I have a question about how to use .grid_forget (in python/TK)
      >
      I need to work on grid repeatly. everytime when a button is pressed,
      the rows of grid is different. such like, first time, it generate 10
      rows of data.
      2nd time, it maybe only 5 rows. so I need a way to RESET the grid data
      every time. how can I do it? by grid_forger()?, then would anyone can
      help on
      how to use grid_forget()
      the sample code as following:
      >
      #####begin of program######## #######
      >
      from Tkinter import *
      def mygrid(text):
      ######## how to use grid_forget() to clean the grid??######### ##
      rows = []
      count=int(text)
      for i in range(count):
      cols = []
      for j in range(4):
      e = Entry(frame3, relief=RIDGE)
      e.grid(row=i, column=j, sticky=NSEW)
      e.insert(END, '%d.%d' % (i, j))
      cols.append(e)
      rows.append(col s)
      >
      >
      root=Tk()
      >
      frame1=Frame(ro ot, width=150, height=100)
      frame1.pack()
      >
      text=Entry(fram e1)
      text.pack(side= LEFT)
      >
      button=Button(f rame1, text='generate grid', command=(lambda :
      mygrid(text.get ())))
      >
      button.pack()
      >
      frame2=Frame(ro ot, width=150, height=100)
      frame2.pack()
      >
      button2=Button( frame2, text='exit', command=root.qu it)
      button2.pack()
      >
      frame3=Frame(ro ot, width=150, height=300)
      frame3.pack()
      >
      root.mainloop()
      >
      #####end of program######## #######

      Using grid_forget() is probably optimization overkill, but may be handy
      for slower computers where you can watch the widgets appear one by one
      (older than about 5 years--for example original mac ibook). Also, you
      should get a good book on Tkinter because your design here will pretty
      difficult to maintain and is not very flexible.

      But...if you want to know how it might be done with grid_forget using
      the code you already have (i.e. making widgets only if necessary):


      #START#
      from Tkinter import *
      from tkMessageBox import showerror
      def mygrid(text):
      ######## how to use grid_forget() to clean the grid??######### ##
      numrows = len(frame3.rows )
      try:
      count=int(text)
      except:
      showerror('Entr y Error',
      '''Hey, "%s" don't make an int, Fool!''' % text,
      parent=frame3)
      return 'break'
      for i in range(count):
      if i < numrows:
      cols = frame3.rows[i]
      else:
      cols = [Entry(frame3, relief=RIDGE) for j in range(4)]
      frame3.rows.app end(cols)
      for j in range(4):
      e = cols[j]
      e.grid(row=i, column=j, sticky=NSEW)
      e.delete(0,END)
      e.insert(END, '%d.%d' % (i, j))
      for i in range(i+1, numrows):
      for e in frame3.rows[i]:
      e.grid_forget()


      root=Tk()

      frame1=Frame(ro ot, width=150, height=100)
      frame1.pack()

      text=Entry(fram e1)
      text.pack(side= LEFT)

      button=Button(f rame1, text='generate grid', command=(lambda :
      mygrid(text.get ())))

      button.pack()

      frame2=Frame(ro ot, width=150, height=100)
      frame2.pack()

      button2=Button( frame2, text='exit', command=root.qu it)
      button2.pack()

      frame3=Frame(ro ot, width=150, height=300)
      # adding an attribute here
      frame3.rows = []
      frame3.pack()

      root.mainloop()
      #END#

      Notice also the necessity for the "e.delete(0 , END)" line to get the
      desired text in the entries.

      Also demonstrated is how to handle poor input.

      *Note*
      Remember to always call the user "Fool" when he does something stupid.


      James

      Comment

      • Ray

        #4
        Re: TK-grid problem, please help

        Hi, Thanks for the help!


        Anton Vredegoor wrote:
        Ray wrote:
        >
        >hi, I have a question about how to use .grid_forget (in python/TK)
        >>
        >I need to work on grid repeatly. everytime when a button is pressed,
        >the rows of grid is different. such like, first time, it generate 10
        >rows of data.
        >2nd time, it maybe only 5 rows. so I need a way to RESET the grid data
        >every time. how can I do it? by grid_forger()?, then would anyone can
        >help on
        >how to use grid_forget()
        >the sample code as following:
        >
        I'm not sure if it solves your problem but this modification of your
        code at least *looks* like it works better. The entries are completely
        destroyed so that the next time you call the function they can be
        recreated.
        >
        The trick I am using is to use a list in the arguments of the function
        but that is a bit of a hack, the list 'remembers' its state from the
        last time the function was called, I think one should use classes for
        bookkeeping such things instead.
        >
        from Tkinter import *
        >
        def mygrid(text,M = []):
        ######## how to use grid_forget() to clean the grid??######### ##
        while M:
        x = M.pop()
        x.destroy()
        rows = []
        count=int(text)
        for i in range(count):
        cols = []
        for j in range(4):
        e = Entry(frame3, relief=RIDGE)
        M.append(e)
        e.grid(row=i, column=j, sticky=NSEW)
        e.insert(END, '%d.%d' % (i, j))
        cols.append(e)
        rows.append(col s)
        >
        >
        root=Tk()
        >
        frame1=Frame(ro ot, width=150, height=100)
        frame1.pack()
        >
        text=Entry(fram e1)
        text.pack(side= LEFT)
        >
        button=Button(f rame1, text='generate grid', command=(lambda :
        mygrid(text.get ())))
        >
        button.pack()
        >
        frame2=Frame(ro ot, width=150, height=100)
        frame2.pack()
        >
        button2=Button( frame2, text='exit', command=root.qu it)
        button2.pack()
        >
        frame3=Frame(ro ot, width=150, height=300)
        frame3.pack()
        >
        root.mainloop()
        >
        A.

        Comment

        • Ray

          #5
          Re: TK-grid problem, please help

          Hi, Thanks for the help.


          I was trying to find a book "Python TK" something on last Friday.
          but didn't find it :-)

          I know those codes are in poor design, because I wrote those sample code
          to show the idea about what I need. the real code is working with mysql.

          however, I'm really new in python. (I start learning it on last Wednesday).

          Thanks again for the help!

          Ray



          James Stroud wrote:
          >
          >
          Using grid_forget() is probably optimization overkill, but may be handy
          for slower computers where you can watch the widgets appear one by one
          (older than about 5 years--for example original mac ibook). Also, you
          should get a good book on Tkinter because your design here will pretty
          difficult to maintain and is not very flexible.
          >
          But...if you want to know how it might be done with grid_forget using
          the code you already have (i.e. making widgets only if necessary):
          >
          >
          #START#
          from Tkinter import *
          from tkMessageBox import showerror
          def mygrid(text):
          ######## how to use grid_forget() to clean the grid??######### ##
          numrows = len(frame3.rows )
          try:
          count=int(text)
          except:
          showerror('Entr y Error',
          '''Hey, "%s" don't make an int, Fool!''' % text,
          parent=frame3)
          return 'break'
          for i in range(count):
          if i < numrows:
          cols = frame3.rows[i]
          else:
          cols = [Entry(frame3, relief=RIDGE) for j in range(4)]
          frame3.rows.app end(cols)
          for j in range(4):
          e = cols[j]
          e.grid(row=i, column=j, sticky=NSEW)
          e.delete(0,END)
          e.insert(END, '%d.%d' % (i, j))
          for i in range(i+1, numrows):
          for e in frame3.rows[i]:
          e.grid_forget()
          >
          >
          root=Tk()
          >
          frame1=Frame(ro ot, width=150, height=100)
          frame1.pack()
          >
          text=Entry(fram e1)
          text.pack(side= LEFT)
          >
          button=Button(f rame1, text='generate grid', command=(lambda :
          mygrid(text.get ())))
          >
          button.pack()
          >
          frame2=Frame(ro ot, width=150, height=100)
          frame2.pack()
          >
          button2=Button( frame2, text='exit', command=root.qu it)
          button2.pack()
          >
          frame3=Frame(ro ot, width=150, height=300)
          # adding an attribute here
          frame3.rows = []
          frame3.pack()
          >
          root.mainloop()
          #END#
          >
          Notice also the necessity for the "e.delete(0 , END)" line to get the
          desired text in the entries.
          >
          Also demonstrated is how to handle poor input.
          >
          *Note*
          Remember to always call the user "Fool" when he does something stupid.
          >
          >
          James

          Comment

          • Hertha Steck

            #6
            Re: TK-grid problem, please help

            Hello,

            Ray schrieb:
            Hi, Thanks for the help.
            >
            >
            I was trying to find a book "Python TK" something on last Friday.
            but didn't find it :-)
            >
            There is only one printed book, all the details here:



            HTH
            Hertha

            Comment

            • Ray

              #7
              Re: TK-grid problem, please help

              Hi Anton,

              Thanks again. This is what I need!
              my problem already solved.

              Ray

              Anton Vredegoor wrote:
              Ray wrote:
              >
              >hi, I have a question about how to use .grid_forget (in python/TK)
              >>
              >I need to work on grid repeatly. everytime when a button is pressed,
              >the rows of grid is different. such like, first time, it generate 10
              >rows of data.
              >2nd time, it maybe only 5 rows. so I need a way to RESET the grid data
              >every time. how can I do it? by grid_forger()?, then would anyone can
              >help on
              >how to use grid_forget()
              >the sample code as following:
              >
              I'm not sure if it solves your problem but this modification of your
              code at least *looks* like it works better. The entries are completely
              destroyed so that the next time you call the function they can be
              recreated.
              >
              The trick I am using is to use a list in the arguments of the function
              but that is a bit of a hack, the list 'remembers' its state from the
              last time the function was called, I think one should use classes for
              bookkeeping such things instead.
              >
              from Tkinter import *
              >
              def mygrid(text,M = []):
              ######## how to use grid_forget() to clean the grid??######### ##
              while M:
              x = M.pop()
              x.destroy()
              rows = []
              count=int(text)
              for i in range(count):
              cols = []
              for j in range(4):
              e = Entry(frame3, relief=RIDGE)
              M.append(e)
              e.grid(row=i, column=j, sticky=NSEW)
              e.insert(END, '%d.%d' % (i, j))
              cols.append(e)
              rows.append(col s)
              >
              >
              root=Tk()
              >
              frame1=Frame(ro ot, width=150, height=100)
              frame1.pack()
              >
              text=Entry(fram e1)
              text.pack(side= LEFT)
              >
              button=Button(f rame1, text='generate grid', command=(lambda :
              mygrid(text.get ())))
              >
              button.pack()
              >
              frame2=Frame(ro ot, width=150, height=100)
              frame2.pack()
              >
              button2=Button( frame2, text='exit', command=root.qu it)
              button2.pack()
              >
              frame3=Frame(ro ot, width=150, height=300)
              frame3.pack()
              >
              root.mainloop()
              >
              A.

              Comment

              • James Stroud

                #8
                Re: TK-grid problem, please help

                Hertha Steck wrote:
                Hello,
                >
                Ray schrieb:
                >
                >Hi, Thanks for the help.
                >>
                >>
                >I was trying to find a book "Python TK" something on last Friday.
                >but didn't find it :-)
                >>
                >
                There is only one printed book, all the details here:
                >

                >
                HTH
                Hertha
                This is inaccurate. There is only one book listed in the wiki. Python
                Programming by Mark Lutz has an excellent Tkinter section as well as an
                *incredible* amount of other information. Also, it has recently been
                updated. It is probably the most relevant book for making a complete
                transition from novice python programmer to expert python programmer.

                James

                Comment

                Working...