Python Tkinter OptionMenu: Changing, deleting, adding more choice options

Friday, June 19, 2009

In my last blog related to Python Tkinter OptionMenu, we have seen the way to add options using the OptionMenu object and display them, in this listing, we will see how to add/change/delete options from OptionMenu on the fly. The python tkinter OptionMenu is a convenient object for holding multiple choice values, typically this is used in GUI programming to enable an user to select a value among multiple choices.

However, as I thought initially, its not a trivial task to edit/delete/add new options to OptionMenu since there isn't a convenient method to do so, initially when you create an OptionMenu, you need to create that object with all possible choices and then to change/delete/add more choices, one should use the add_command method of OptionMenu's menu object (which is a tkinter Menu), the below code does the same.

1.  # File name:
2. # Changing/adding/deleting options to OptionMenu on the fly
3. # Author: S.Prasanna
5. from Tkinter import *
6. import tkMessageBox
8. def displayOption():
9. """ Display the OptionMenu selection. """
11. global optionMenuWidget, DEFAULTVALUE_OPTION
13. if (optionMenuWidget.cget("text") == DEFAULTVALUE_OPTION):
14. tkMessageBox.showerror("Tkinter OptionMenu Widget", "Select a valid option.")
15. else:
16. tkMessageBox.showinfo("Tkinter OptionMenu Widget", "OptionMenu value = " + optionMenuWidget.cget("text"))
18. def addMenuOptions():
19. """ Add Menu options dynamically """
21. global optionMenuWidget
23. optionMenuWidget["menu"].delete(0, END)
24. # Add options from 1 to 5
25. for i in range(1, 6):
26. optionMenuWidget["menu"].add_command(label=i, command=lambda temp = i: optionMenuWidget.setvar(optionMenuWidget.cget("textvariable"), value = temp))
28. if __name__ == "__main__":
30. root = Tk()
31. DEFAULTVALUE_OPTION = "Select an option."
33. root.title("Tkinter OptionMenu Widget")
34. root["padx"] = 40
35. root["pady"] = 20
37. # Create an Option frame to hold the option Label and the optionMenu widget
38. optionFrame = Frame(root)
40. #Create a Label in textFrame
41. optionLabel = Label(optionFrame)
42. optionLabel["text"] = "OptionsMenu demo"
43. optionLabel.pack(side=LEFT)
45. # Create an optionMenu Widget in the optionFrame
46. optionTuple = ("",)
48. defaultOption = StringVar()
49. optionMenuWidget = apply(OptionMenu, (optionFrame, defaultOption) + optionTuple)
50. addMenuOptions()
51. defaultOption.set(DEFAULTVALUE_OPTION)
52. optionMenuWidget["width"] = 15
53. optionMenuWidget.pack(side=LEFT)
55. optionFrame.pack()
57. button = Button(root, text="Submit", command=displayOption)
58. button.pack()
60. root.mainloop()
Sample Output:


The code here is similar to the one discussed in the tkinter OptionMenu and its usage blog where we create an OptionMenu and initialize it with a list of choice options, here we do the same where we initialize the OptionMenu with an empty value "" (line 46), then remove that value (line 23) and add more options (line 24 - 26).

The code segment which is of interest here is
23.     optionMenuWidget["menu"].delete(0, END)
24. # Add options from 1 to 5
25. for i in range(1, 6):
26. optionMenuWidget["menu"].add_command(label=i, command=lambda temp = i: optionMenuWidget.setvar(optionMenuWidget.cget("textvariable"), value = temp))
Line 23 removes all option choices from the OptionsMenu's menu property, line 25 - 26 adds five option choices using the add_command method of OptionMenu's menu property, the code
command=lambda temp = i: optionMenuWidget.setvar(optionMenuWidget.cget("textvariable"), value = temp)
initializes a command which executes an inline lambda function which sets the OptionMenu's control variable (got using optionMenuWidget.cget("textvariable")) to the option label or choice value (using the setvar method).

MS&E235 Internet Commerce course in Stanford

Monday, June 15, 2009

Finally after a grueling past couple of months, I completed my first course in Stanford, Internet Commerce (MS&E235) taught by Prof.Ashish Goel, it was a very interesting one, the learning part was mainly focused on the mathematics and economics of Internet commerce and its applications with a team project.

Let me dig little deep into it, Internet Commerce has evolved over the years with online advertising growing as a profitable business model, its easy for any small scale industry to grow by connecting with their customers online, with Social Networking, Web 2.0 getting widespread acceptance, now its possible to explore the length and breadth of opportunities online in a cost effective way than traditional commerce.

The way Google has reinvented search and influenced millions, so was the growth of Internet commerce and given its importance, this course can be very strategic for anyone who wants to explore the world of Internet commerce, There are lots of interesting concepts I learnt, some of them worth mentioning were

1. Advertising and Auctions: Here the focus was primarily on CPC/CPM based ads, first price/second price/next price/laddered auctions, the way bids are ordered based on several factors like bidding price and quality scores, how positioning affects CTR, etc.

2. Long tails: This is a fundamental concept which anyone should be aware of, the long tails is a business strategy which ensures that the least popular business items are also sold based on individual's interest, as one can see that these things contribute to the business greatly as it was shown that 25% of Google searches were unique (or 1 in 4) and these were influential to their business, more about long tails can be found here.

3. Page rank: This is the highlight of the course where you will learn in detail about Google's unique idea of page rank where web pages are assigned a reputation score which not only helps to identify top results for web searches, but also makes it effective to order and retrieve pages based on their rank, it always helps to learn more about the paper published by stalwarts like Larry Page, Sergey Brin and Rajeev Motwani.

4. Reputation and Recommendation systems: These days where competitive business advantage depends mainly on reputation and recommendation systems, this is another vital area which was covered quite extensively, some of the examples are page rank and HITS.

The practical problems solved were quite intriguing and the feeling after cracking them was something similar to solving a research problem, if you are in advertising/E-business profession, then it is for you, I will also blog about the concepts mentioned above to have a handy online reference, keep visiting.

The Google Online Marketing Challenge Experience

Saturday, June 13, 2009

This week we completed our Google Online Marketing Challenge campaign for a startup business in Palo Alto, CA. It was a very interesting experience where I got a chance to work with other Stanford students, learning a lot about Google Adwords in general, the campaign also resulted in tangible business results to our client, making it a great success.

Online Advertising has a huge business potential with advertising sales growing at 20% annually, on any given day there is a great chance that one of the web pages we surf in the Internet has some form of advertisement, therefore its good to get a grasp of it and Google Online Marketing Challenge provides provides a great opportunity for students to work together as a team partnering with some small scale business to help them devise an effective online marketing campaign through Google Adwords.

More details about the challenge can be found here, if you are a business student, you should go for this at some point of time, you will receive funding from Google to carry out your campaign with rewards for top campaigns, let me share some of our experiences on this challenge with useful pointers at the end.

The Pre-Campaign Strategy report provides a perfect platform to plan things before you launch the campaign, most importantly the pre-campaign strategy report carries some points, therefore its imperative to have a clear pre-campaign strategy before executing it, the main focus of this report is highlighting more about the business you selected, their strengths, scope of improvement, the Adwords strategy you are planning (like CPC or CPM based Ads, Adgroups, CTR expectations, geo-targeting), once you have a clear-cut strategy devised for the campaign and document it, it will help a long way to launch a successful campaign.

Our Campaign was an exciting experience where we got a chance to play with Adwords from changing/pausing keywords, refining, revisiting and redesigning Ads, increasing/reducing bids, geo targeting based on Google analytics reports and so on, there are a lot of parameters which will give a meaningful measure on how your campaign was progressing, thanks to Google Adwords and Analytics report, our campaign strategy was three fold, Observe, Improve and Optimize.

The first week was focused on observing Adwords/Analytics data, experimenting with keywords, identifying top performing Adgroups and Keywords, the second week was focused on improvements where we applied some techniques to improve our campaign performance and the final week was focused on optimization where we focused on making the best use of our remaining budget by fine tuning our settings for maximized performance.

The Post Campaign Summary report not only summarizes your campaign, but it also includes a section on your learning experience from the campaign which provides a great chance to share your learning from the campaign like managing the campaign, difficulties encountered and so on, a very important report which carries a lot of weightage

Useful Pointers:

Student Guides: (all support documentation to help you with the challenge)
Submitting/Uploading your reports for Google Online Marketing Challenge: (Pre Campaign Strategy and Post Campaign summary report)

Hope you are raring to go ahead with the challenge, start now.

Copyright © 2016 Prasanna Seshadri,, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.