hi, i am very new in python. can anyone tell me that how to create dynamic dropdown box in python scripting.(triple dropdown box)
sharma_vivek82 0 Newbie Poster
Ene Uran 638 Posting Virtuoso
What GUI library are you using? I am a little familiar with Tkinter and wxPython and might be able to help. There are others I am not familiar with.
sharma_vivek82 0 Newbie Poster
i am working in ubuntu linux. and using python 2.4.2.
sharma_vivek82 0 Newbie Poster
simple code for creating dropdown box using javascript.
the html code for this, is
<html>
<head>
<title></title>
<script language="javascript" src="list.js"></script>
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000" onload="fillCategory();">
<FORM name="drop_list" >
<SELECT NAME="Category" onChange="SelectDistrict();" >
<Option value="">States</option>
</SELECT>
<SELECT id="District" NAME="District" onChange="SelectCollege();">
<Option value="">Districts</option>
</SELECT>
<SELECT id="College" NAME="College">
<Option value="">College</option>
</SELECT>
<input type="button" name="go" value="Value Selected"
onclick="alert(document.drop_list.College.options[document.drop_list.College.selectedIndex].value);">
</form>
</body>
</html>
here i am calling javascript writen in another file list.js. The content of list.js is following
function fillCategory(){
// this function is used to fill the category list on load
addOption(document.drop_list.Category, "Karnataka", "Karnataka", "");
addOption(document.drop_list.Category, "Jharkhand", "Jharkhand", "");
addOption(document.drop_list.Category, "Hariyana", "Hariyana", "");
}
function SelectDistrict(){
// ON selection of category this function will work
removeAllOptions(document.drop_list.District);
addOption(document.drop_list.District, "", "District", "");
if(document.drop_list.Category.value == 'Karnataka'){
addOption(document.drop_list.District,"Bangalore", "Bangalore");
addOption(document.drop_list.District,"Kolar", "Kolar");
addOption(document.drop_list.District,"Hubali", "Hubali");
}
if(document.drop_list.Category.value == 'Jharkhand'){
addOption(document.drop_list.District,"Ranchi", "Ranchi");
addOption(document.drop_list.District,"Jamshedpur", "Jamshedpur");
addOption(document.drop_list.District,"Hazbagh", "Hazbagh", "");
}
if(document.drop_list.Category.value == 'Hariyana'){
addOption(document.drop_list.District,"Noida", "Noida");
addOption(document.drop_list.District,"Gurgaon", "Gurgaon");
addOption(document.drop_list.District,"Panipat", "Panipat");
}
}
function SelectCollege(){
// ON selection of District this function will work
removeAllOptions(document.drop_list.College);
addOption(document.drop_list.College, "", "College", "");
if(document.drop_list.District.value == 'Bangalore'){
addOption(document.drop_list.College,"BIT Bangalore", "BIT Bangalore");
addOption(document.drop_list.College,"MVIT Bangalore", "MVIT Bangalore");
}
if(document.drop_list.District.value == 'Kolar'){
addOption(document.drop_list.College,"BIT Kolar", "BIT Kolar");
addOption(document.drop_list.College,"BIT Kolar", "BIT Kolar");
}
if(document.drop_list.District.value == 'Hubali'){
addOption(document.drop_list.College,"YMCA", "YMCA");
addOption(document.drop_list.College,"IIT", "IIT");
}
if(document.drop_list.District.value == 'Ranchi'){
addOption(document.drop_list.College,"ZXA", "ZXA");
addOption(document.drop_list.College,"DSA", "DSA");
}
if(document.drop_list.District.value == 'Jamshedpur'){
addOption(document.drop_list.College,"QWE", "QWE");
addOption(document.drop_list.College,"ZXC", "ZXC");
}
if(document.drop_list.District.value == 'Hazbagh'){
addOption(document.drop_list.College,"ZDC", "ZDC");
addOption(document.drop_list.College,"FGV", "FGV");
}
if(document.drop_list.District.value == 'Noida'){
addOption(document.drop_list.College,"uio", "uio");
addOption(document.drop_list.College,"klj", "klj");
}
if(document.drop_list.District.value == 'Gurgaon'){
addOption(document.drop_list.College,"yhn", "yhn");
addOption(document.drop_list.College,"fgh", "fgh");
}
if(document.drop_list.District.value == 'Panipat'){
addOption(document.drop_list.College,"olm", "olm");
addOption(document.drop_list.College,"bnm", "bnm");
}
}
//////////////////
function removeAllOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
//selectbox.options.remove(i);
selectbox.remove(i);
}
}
function addOption(selectbox, value, text )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}
this is very simple to understand it.
If any one have code to create dropdown box in Python script plz send me....
Edit: Added code tags vegaseat
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
Here is an example using wxPython. Once you have Python installed you need to download wxPython appropriate for your system and Python version, it's free!
# the wxChoice() widget, a simple dropdown box
# wx.Choice(parent, id, pos, size, choices, style)
# tested with Python24 and wxPython26
# should work on Windows, Linux, and Unix systems
import wx
class MyFrame(wx.Frame):
""" class MyFrame inherits wxFrame, uses default ID of -1"""
def __init__(self ):
wx.Frame.__init__(self, None, -1, 'wxChoice test', size=(300, 150))
colorList = ['red', 'green', 'blue', 'yellow','white']
# create the dropdown box
self.choice1 = wx.Choice(self, -1, choices=colorList)
# select item 1 = 'green' to show
self.choice1.SetSelection(1)
# set focus to receive optional keyboard input
# eg. type r for red, y for yellow
self.choice1.SetFocus()
# new event handler wxPython version 2.5 and higher
self.choice1.Bind(wx.EVT_CHOICE, self.onChoice)
def onChoice(self, event):
'''get the slected color choice'''
self.color = self.choice1.GetStringSelection()
self.SetTitle(self.color) # test
# this is only a small application
application = wx.PySimpleApp()
# call class MyFrame
frame1 = MyFrame()
# show the frame
frame1.Show(True)
# start the event loop
application.MainLoop()
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.