Selecting/Invoking Menubuttons in python Tkinter -
i trying create menu calls sql database life expectancy data across world. i'm having trouble menubutton, want set first value ("no region selected") initial value. there 3 things want: how set value default (ideally equivalent of invoked normal radiobuttons), there better want option menubutton, , there decent web page can me (tutorialspoint got me in initial direction, unless i'm looking in wrong places, can't find how select , invoke radiobuttons in menubutton).
ideally, i'd advise on how not have menuoptions stretch everywhere too. limit of 20?
here's of stripped down piece of code.
edit: i've found solution select/invoke issues. mb.menu.invoke(0)
from tkinter import * import tkmessagebox import tkinter import sqlite3 def selectstuff(): selectionregion = str(countryvar.get()) mb.config(text = selectionregion) sqllocation = 'alldata/lifeexpectency.sqlite' sqldb = sqlite3.connect(sqllocation) cursor = sqldb.cursor() root = tk() framecountry = frame(root) framecountry.pack() stringcountry= stringvar() labelcountry = label(framecountry, textvariable=stringcountry) stringcountry.set("select region") labelcountry.pack() '''-------------------------------''' mb = menubutton ( framecountry, relief=raised, activebackground="white") mb.grid() mb.menu = menu ( mb, tearoff = 0 ) mb["menu"] = mb.menu sql1 = "select country total_lifespan" countryvar = stringvar() mb.menu.add_radiobutton ( label="no region selected", variable =countryvar, value = "no region selected", command =selectstuff) try: cursor.execute(sql1) results = cursor.fetchall() row in results: mb.menu.add_radiobutton ( label=row, variable =countryvar, value = row, command =selectstuff) except: print "error: unable retrieve data." mb.pack() '''-------------------------------''' #edit: mb.menu.invoke(0) #/edit: root.mainloop() sqldb.close()
Comments
Post a Comment