I am trying to put together my first little program, but I keep having some difficulties. I am using data from an SQLite DB, but I changed the code below a bit so it's usable without the DB itself. The list_base
and List_sold_at_base
functions usually would pull data from a DB (base and commodities info from the Freelancer game, so I can list what commodities are sold on what base).
What the program should do is build up a list of all bases when the 'bases' button is pressed and put this in the Combobox
.
When selecting an option from the Combobox
this selection should be stored in the variable v
, and update when another base is selected. This works. the variable v
is a str
type so far.
The Goods sold at base
button calls a function that takes v
and uses it to check in de DB what goods are sold at this base, and prints this out in a list.
However, now the variable v
isn't a string type anymore (which it should be for the function to work properly), but an instance
. Somehow this gets changed but I can't figure out where and why. I put in a couple of print statements to see how and where, but they don't really show it all that well.
from Tkinter import *
import csv, sqlite3, sys
from ttk import Combobox
root = Tk()
v = StringVar()
def on_field_change(index, value, op):
print "combobox updated to ", boxBases.get()
v = boxBases.get()
print 'v is', v
print type(v)
def List_bases():
dropdownBases = []
for i in ['base1', 'base2', 'base3', 'base4',]:
dropdownBases.append(i)
return dropdownBases
def List_sold_at_base(selbase):
goodsSoldAtBase = []
sb=selbase
print "selbase value (v) is", selbase
print "selbase type is ", type(selbase)
dataFromDB = [['base1', 'Cobalt 275', 'Copper 180'],['base2', 'High Temp Alloy 280', 'Mining Machinery 130'], ['base3', 'H-Fuel 240', 'Oxygen 6', 'Water 18'], ['base4', 'Hydrocarbons 40', 'Polymers 107']]
for i in dataFromDB:
if i[0] == sb:
goodsSoldAtBase.append(i)
print "goods sold at base list: ", goodsSoldAtBase
print " "
print v
print type(v)
def base():
dropdownBases = List_bases()
boxBases.config(values=dropdownBases)
print "bases!"
def goods():
List_sold_at_base(v)
print '=========='
dropdownBases = []
v.trace('w',on_field_change)
boxBases = Combobox(root, textvar=v, values=dropdownBases)
boxBases.grid(row=4, column=3, sticky='w', padx=5, pady=5)
baseButton=Button(root, text="bases", command=base)
baseButton.grid(row=3, column=0, sticky='w', padx=5, pady=5)
goodsButton = Button(root, text="Goods sold at base", command=goods)
goodsButton.grid(row=3, column=2, sticky='w', padx=5, pady=5)
root.mainloop()