Grid View (Table list) in tkinter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AruzV
    New Member
    • Feb 2010
    • 4

    Grid View (Table list) in tkinter

    Is there any data grid sample for tkinter?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    AruzV,

    Can you be more specific? Are you looking for an example of a Tkinter widget using the grid geometry manager?

    Comment

    • AruzV
      New Member
      • Feb 2010
      • 4

      #3
      I need a sample table viewer in tkinter like excell or any database viewer. It must have Columns and rows and headers. Like this.

      _|_A_|_B_|_C_|_ D_|_E_|
      1|___|___|___|_ __|___|
      2|___|___|___|_ __|___|
      3|___|___|___|_ __|___|
      4|___|___|___|_ __|___|
      5|___|___|___|_ __|___|

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Have you tried to code this for yourself? Do you know anything about Tkinter? Is this homework? I can provide code to parametrically build a grid like you describe, but we are not here to provide code to people without some effort on their part. I attached an image of a grid of Tkinter.Entry widgets. The column and row headers are not modifiable. Characters can be entered into the open grids. The fields are bound to '<FocusOut>' (prints field contents if not blank, destroys the top level widget if 'exit' is entered).

        BV - Moderator
        Attached Files

        Comment

        • AruzV
          New Member
          • Feb 2010
          • 4

          #5
          I am newer in python and tkinter. This is not my homework. I am not a student. I couldn't find any widget. Is there a widget for table view like this:

          >>>dview=tkinte r.tableview()
          >>>dview.pack ()
          >>>dview.cell[2][3].text="some string"
          >>>dview.cell[2][4].image="someima ge.png"
          >>>dbutton=dvie w.cell[1][1].button

          but I couldnt find in google. There are some samples but they are not enough for me.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            In that case, I have no problem posting sample code. Type "demo" into one of the cells and tab out to see what happens.
            Code:
            import Tkinter
            from time import sleep
            
            textFont1 = ("Arial", 10, "bold italic")
            textFont2 = ("Arial", 16, "bold")
            textFont3 = ("Arial", 8, "bold")
            
            class LabelWidget(Tkinter.Entry):
                def __init__(self, master, x, y, text):
                    self.text = Tkinter.StringVar()
                    self.text.set(text)
                    Tkinter.Entry.__init__(self, master=master)
                    self.config(relief="ridge", font=textFont1,
                                bg="#ffffff000", fg="#000000fff",
                                readonlybackground="#ffffff000",
                                justify='center',width=8,
                                textvariable=self.text,
                                state="readonly")
                    self.grid(column=x, row=y)
            
            class EntryWidget(Tkinter.Entry):
                def __init__(self, master, x, y):
                    Tkinter.Entry.__init__(self, master=master)
                    self.value = Tkinter.StringVar()
                    self.config(textvariable=self.value, width=8,
                                relief="ridge", font=textFont1,
                                bg="#ddddddddd", fg="#000000000",
                                justify='center')
                    self.grid(column=x, row=y)
                    self.value.set("")
                    
            class EntryGrid(Tkinter.Tk):
                ''' Dialog box with Entry widgets arranged in columns and rows.'''
                def __init__(self, colList, rowList, title="Entry Grid"):
                    self.cols = colList[:]
                    self.colList = colList[:]
                    self.colList.insert(0, "")
                    self.rowList = rowList
                    Tkinter.Tk.__init__(self)
                    self.title(title)
            
                    self.mainFrame = Tkinter.Frame(self)
                    self.mainFrame.config(padx='3.0m', pady='3.0m')
                    self.mainFrame.grid()
                    self.make_header()
            
                    self.gridDict = {}
                    for i in range(1, len(self.colList)):
                        for j in range(len(self.rowList)):
                            w = EntryWidget(self.mainFrame, i, j+1)
                            self.gridDict[(i-1,j)] = w.value
                            def handler(event, col=i-1, row=j):
                                return self.__entryhandler(col, row)
                            w.bind(sequence="<FocusOut>", func=handler)
                    self.mainloop()
            
                def make_header(self):
                    self.hdrDict = {}
                    for i, label in enumerate(self.colList):
                        def handler(event, col=i, row=0, text=label):
                            return self.__headerhandler(col, row, text)
                        w = LabelWidget(self.mainFrame, i, 0, label)
                        self.hdrDict[(i,0)] = w
                        w.bind(sequence="<KeyRelease>", func=handler)
                        
                    for i, label in enumerate(self.rowList):
                        def handler(event, col=0, row=i+1, text=label):
                            return self.__headerhandler(col, row, text)
                        w = LabelWidget(self.mainFrame, 0, i+1, label)
                        self.hdrDict[(0,i+1)] = w
                        w.bind(sequence="<KeyRelease>", func=handler)
            
                def __entryhandler(self, col, row):
                    s = self.gridDict[(col,row)].get()
                    if s.upper().strip() == "EXIT":
                        self.destroy()
                    elif s.upper().strip() == "DEMO":
                        self.demo()
                    elif s.strip():
                        print s
            
                def demo(self):
                    ''' enter a number into each Entry field '''
                    for i in range(len(self.cols)):
                        for j in range(len(self.rowList)):
                            sleep(0.25)
                            self.set(i,j,"")
                            self.update_idletasks()
                            sleep(0.1)
                            self.set(i,j,i+1+j)
                            self.update_idletasks()
            
                def __headerhandler(self, col, row, text):
                    ''' has no effect when Entry state=readonly '''
                    self.hdrDict[(col,row)].text.set(text)
            
                def get(self, x, y):
                    return self.gridDict[(x,y)].get()
            
                def set(self, x, y, v):
                    self.gridDict[(x,y)].set(v)
                    return v
            
            if __name__ == "__main__":
                cols = ['A', 'B', 'C', 'D']
                rows = ['1', '2', '3', '4']
                app = EntryGrid(cols, rows)

            Comment

            • AruzV
              New Member
              • Feb 2010
              • 4

              #7
              Thanks bvdet this is helpful for me.

              Comment

              • neilhmurphy
                New Member
                • Mar 2010
                • 5

                #8
                Thanks

                I have found other examples (mostly inadequate), but none showed how to get data into the script.

                Comment

                • muco
                  New Member
                  • Mar 2010
                  • 1

                  #9
                  I desided to use tkinter treeview.

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    I have found other examples (mostly inadequate), but none showed how to get data into the script.
                    What data are you trying to get into "the script"?

                    Comment

                    • neilhmurphy
                      New Member
                      • Mar 2010
                      • 5

                      #11
                      No laughing! I want to write a version of Quicken. I was forced to upgrade to Q2010 from Q2001 when I got Win7 64 bit. Q2010 is so awful that I must do something. I have started using GnuCash which is awkward, but a lot better than Q2010.

                      Comment

                      • neilhmurphy
                        New Member
                        • Mar 2010
                        • 5

                        #12
                        Would you add a scroll bar to your example?

                        Comment

                        • bvdet
                          Recognized Expert Specialist
                          • Oct 2006
                          • 2851

                          #13
                          I have never done a scrollbar. I will try adding one when I get some spare time.

                          Comment

                          • neilhmurphy
                            New Member
                            • Mar 2010
                            • 5

                            #14
                            Thanks, you can imagine my difficulties.

                            Comment

                            • megumi
                              New Member
                              • Dec 2018
                              • 1

                              #15
                              can you pls help me,i need to put elemeents from a list into the table and cant figure it out.

                              Comment

                              Working...