0

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()
nbro
  • 15,395
  • 32
  • 113
  • 196
Bowerick
  • 317
  • 1
  • 3
  • 15
  • Possible duplicate of [Tkinter IntVar returning PY\_VAR0 instead of value](http://stackoverflow.com/questions/24768455/tkinter-intvar-returning-py-var0-instead-of-value) – nbro Mar 16 '17 at 23:23
  • Thank you, I will have a look at it later! – Bowerick Mar 17 '17 at 07:49
  • I don't understand why you want to change it to string? You can always get the value of it using `v.get()` and change its value using `v.set("new_value")`. – Lafexlos Mar 17 '17 at 15:00

1 Answers1

0

Thanks for the input. I got it to work by trying around with the .get() a bit more and taking out 1 function that basically just called another function. I cut out the middle man (goods() ).

from Tkinter import *
import csv, sqlite3, sys
from ttk import Combobox

root = Tk()

v = StringVar()


def on_field_change(index, value, op):
    v = boxBases.get()
    print 'v is', v


def List_bases():
    dropdownBases = []
    for i in ['base1', 'base2', 'base3', 'base4',]:
        dropdownBases.append(i)
    return dropdownBases

def List_sold_at_base():
    goodsSoldAtBase = []
    sb=v.get()
    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

def base():
    dropdownBases = List_bases()
    boxBases.config(values=dropdownBases)
    print "bases!"

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=List_sold_at_base)
goodsButton.grid(row=3, column=2, sticky='w', padx=5, pady=5)
root.mainloop()
Bowerick
  • 317
  • 1
  • 3
  • 15