By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,842 Members | 2,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,842 IT Pros & Developers. It's quick & easy.

Grid View (Table list) in tkinter

P: 4
Is there any data grid sample for tkinter?
Feb 23 '10 #1
Share this Question
Share on Google+
14 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
AruzV,

Can you be more specific? Are you looking for an example of a Tkinter widget using the grid geometry manager?
Feb 23 '10 #2

P: 4
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|___|___|___|___|___|
Feb 24 '10 #3

bvdet
Expert Mod 2.5K+
P: 2,851
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 Images
File Type: gif EntryGrid.gif (5.9 KB, 22021 views)
Feb 25 '10 #4

P: 4
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=tkinter.tableview()
>>>dview.pack()
>>>dview.cell[2][3].text="some string"
>>>dview.cell[2][4].image="someimage.png"
>>>dbutton=dview.cell[1][1].button

but I couldnt find in google. There are some samples but they are not enough for me.
Feb 25 '10 #5

bvdet
Expert Mod 2.5K+
P: 2,851
In that case, I have no problem posting sample code. Type "demo" into one of the cells and tab out to see what happens.
Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. from time import sleep
  3.  
  4. textFont1 = ("Arial", 10, "bold italic")
  5. textFont2 = ("Arial", 16, "bold")
  6. textFont3 = ("Arial", 8, "bold")
  7.  
  8. class LabelWidget(Tkinter.Entry):
  9.     def __init__(self, master, x, y, text):
  10.         self.text = Tkinter.StringVar()
  11.         self.text.set(text)
  12.         Tkinter.Entry.__init__(self, master=master)
  13.         self.config(relief="ridge", font=textFont1,
  14.                     bg="#ffffff000", fg="#000000fff",
  15.                     readonlybackground="#ffffff000",
  16.                     justify='center',width=8,
  17.                     textvariable=self.text,
  18.                     state="readonly")
  19.         self.grid(column=x, row=y)
  20.  
  21. class EntryWidget(Tkinter.Entry):
  22.     def __init__(self, master, x, y):
  23.         Tkinter.Entry.__init__(self, master=master)
  24.         self.value = Tkinter.StringVar()
  25.         self.config(textvariable=self.value, width=8,
  26.                     relief="ridge", font=textFont1,
  27.                     bg="#ddddddddd", fg="#000000000",
  28.                     justify='center')
  29.         self.grid(column=x, row=y)
  30.         self.value.set("")
  31.  
  32. class EntryGrid(Tkinter.Tk):
  33.     ''' Dialog box with Entry widgets arranged in columns and rows.'''
  34.     def __init__(self, colList, rowList, title="Entry Grid"):
  35.         self.cols = colList[:]
  36.         self.colList = colList[:]
  37.         self.colList.insert(0, "")
  38.         self.rowList = rowList
  39.         Tkinter.Tk.__init__(self)
  40.         self.title(title)
  41.  
  42.         self.mainFrame = Tkinter.Frame(self)
  43.         self.mainFrame.config(padx='3.0m', pady='3.0m')
  44.         self.mainFrame.grid()
  45.         self.make_header()
  46.  
  47.         self.gridDict = {}
  48.         for i in range(1, len(self.colList)):
  49.             for j in range(len(self.rowList)):
  50.                 w = EntryWidget(self.mainFrame, i, j+1)
  51.                 self.gridDict[(i-1,j)] = w.value
  52.                 def handler(event, col=i-1, row=j):
  53.                     return self.__entryhandler(col, row)
  54.                 w.bind(sequence="<FocusOut>", func=handler)
  55.         self.mainloop()
  56.  
  57.     def make_header(self):
  58.         self.hdrDict = {}
  59.         for i, label in enumerate(self.colList):
  60.             def handler(event, col=i, row=0, text=label):
  61.                 return self.__headerhandler(col, row, text)
  62.             w = LabelWidget(self.mainFrame, i, 0, label)
  63.             self.hdrDict[(i,0)] = w
  64.             w.bind(sequence="<KeyRelease>", func=handler)
  65.  
  66.         for i, label in enumerate(self.rowList):
  67.             def handler(event, col=0, row=i+1, text=label):
  68.                 return self.__headerhandler(col, row, text)
  69.             w = LabelWidget(self.mainFrame, 0, i+1, label)
  70.             self.hdrDict[(0,i+1)] = w
  71.             w.bind(sequence="<KeyRelease>", func=handler)
  72.  
  73.     def __entryhandler(self, col, row):
  74.         s = self.gridDict[(col,row)].get()
  75.         if s.upper().strip() == "EXIT":
  76.             self.destroy()
  77.         elif s.upper().strip() == "DEMO":
  78.             self.demo()
  79.         elif s.strip():
  80.             print s
  81.  
  82.     def demo(self):
  83.         ''' enter a number into each Entry field '''
  84.         for i in range(len(self.cols)):
  85.             for j in range(len(self.rowList)):
  86.                 sleep(0.25)
  87.                 self.set(i,j,"")
  88.                 self.update_idletasks()
  89.                 sleep(0.1)
  90.                 self.set(i,j,i+1+j)
  91.                 self.update_idletasks()
  92.  
  93.     def __headerhandler(self, col, row, text):
  94.         ''' has no effect when Entry state=readonly '''
  95.         self.hdrDict[(col,row)].text.set(text)
  96.  
  97.     def get(self, x, y):
  98.         return self.gridDict[(x,y)].get()
  99.  
  100.     def set(self, x, y, v):
  101.         self.gridDict[(x,y)].set(v)
  102.         return v
  103.  
  104. if __name__ == "__main__":
  105.     cols = ['A', 'B', 'C', 'D']
  106.     rows = ['1', '2', '3', '4']
  107.     app = EntryGrid(cols, rows)
Feb 25 '10 #6

P: 4
Thanks bvdet this is helpful for me.
Feb 28 '10 #7

P: 5
I have found other examples (mostly inadequate), but none showed how to get data into the script.
Mar 23 '10 #8

P: 1
I desided to use tkinter treeview.
Mar 23 '10 #9

bvdet
Expert Mod 2.5K+
P: 2,851
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"?
Mar 23 '10 #10

P: 5
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.
Mar 24 '10 #11

P: 5
Would you add a scroll bar to your example?
Mar 26 '10 #12

bvdet
Expert Mod 2.5K+
P: 2,851
I have never done a scrollbar. I will try adding one when I get some spare time.
Mar 26 '10 #13

P: 5
Thanks, you can imagine my difficulties.
Mar 27 '10 #14

P: 1
can you pls help me,i need to put elemeents from a list into the table and cant figure it out.
Dec 30 '18 #15

Post your reply

Sign in to post your reply or Sign up for a free account.