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

Tkinter table menu

P: 1
http://bytes.com/topic/python/answer...er#post3716687

I'm a beginner in python trying to use the nice code you posted. How can I add a simple menu to the app? The mainloop is in the Entrygrid, and I was not able to put it somewhere else without affecting the functionality of the program.
Apr 23 '12 #1
Share this Question
Share on Google+
1 Reply

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

Post your reply

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