473,508 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter table menu

1 New Member
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
1 3326
bvdet
2,851 Recognized Expert Moderator Specialist
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

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

Similar topics

1
11570
by: Patrick L. Nolan | last post by:
We have a Tkinter application which has a menubar with cascade submenus. I would like to start the program with one of the submenu items state=DISABLED, then change it to state=NORMAL at a later...
4
4756
by: Torsten Mohr | last post by:
Hi, i want to write an application where i need a Table to display some values. The table should display a list of objects, so to say, a column for each attribute of the object. I'd also...
2
1506
by: Summasummarum | last post by:
Hi, As subj says. A simple menu is defined in a table. But how to extract it? Heres the deal: Table menu has these 3 columns: menuid parent menudesc Ok this should be easy right?...
2
1528
by: Gordon | last post by:
VB.Net 2003 Standard - Creating ASP.Net Web Application In design mode, I can drag a html table from the toolbox to the work area but I can't seem to be able to select individual cells or...
1
1673
by: dkimbrell | last post by:
Hi there, I'm very novice to web design. I'm trying to make a pulldown menu, but the formatting keeps getting screwed up when you roll the mouse over it. Please see www.boundarysys.com for...
1
1763
by: leicklda | last post by:
Hi there, I'm very novice to web design. I'm trying to make a pulldown menu, but the formatting keeps getting screwed up when you roll the mouse over it. Please see www.boundarysys.com for...
1
3615
by: Carl | last post by:
"Chuckk Hubbard" <badmuthahubbard@gmail.comwrites: Try creating the "main" popup menu before the sub-menus, and when instantiating the sub-menus, pass the main menu as the "master" instead of...
2
4157
by: dharmbhav | last post by:
Hello all, I am trying to develop a roll-over menu effect on a page. It works fine with all other browsers except IE6. Can some one please help me? HTML: <div class="menu-item-wrap">...
2
2043
by: xFUNKYFACE | last post by:
I got a table named "language" were I store all the text from my website (http://www.ultimespace.net/menu/). So in a page I called "sort.php" I actually transfert all the values from the user...
0
1118
by: Neven Huynh | last post by:
Hi Everyone, Here i my LINQ query to get record in Table Menu with condition are parentID == 0(get root menu) and ID != (parentID list) (which is parent ID list is are id of menu record that have...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.