Forum

Author Topic: Trying to obtain the selected value, but fails.!!!  (Read 2288 times)

johnmickenzy

  • Newbie
  • *
  • Posts: 1
    • View Profile
Trying to obtain the selected value, but fails.!!!
« on: February 01, 2019, 03:41:53 PM »
Hello there masters of code. I am learning python and progressing through it, and I'm trying to code something up which takes value from one combo box and then according to the values activates another combo box and proceeds to Print out the value of that one onto a print statement.. but It is not working as expected and is giving me errors. I have been tinkering with this since a 4~5 hours now before finally coming up here to ask for remedial tips to get this working. :(

import tkinter as tk
from tkinter import ttk


def callbackFunc(event):

    if Reso.get() == "Crops":
        Crop()
#This is what is not working. Need the CropC.get part to provide selected value.
        print('Data/' + CropC.get() + '/Seed')


app = tk.Tk()
app.geometry('200x100')

crops = []

#Reads from file and populates the list.
with open('Data\file\list.ini') as f:
    for line in f:
        inner_list = [elt.strip() for elt in line.split(' ')]
        crops.append(inner_list)

#Resources
labelTop = tk.Label(app, text = "Select Resource type" )
labelTop.grid(column=0, row=0)
Reso = ttk.Combobox(app, values=[" ","Crops","Other", "Values"])
print(dict(Reso))
Reso.grid(column=0, row=1)
Reso.current(0)
Reso.bind("<<ComboboxSelected>>", callbackFunc)

# Dummy Combo Box to show
labelTop = tk.Label(app, text = "Resource")
labelTop.grid(column=0, row=3)
free = ttk.Combobox(app, values=" ")
free.grid(column=0, row=4)

#Crops
def Crop():
    CropC = ttk.Combobox(app, values=crops)
    print(dict(CropC))
    CropC.grid(column=0, row=4)
    CropC.current(0)
    CropC.bind("<<ComboboxSelected>>", callbackFunc)

# If i Use it here "print('Data/' + CropC.get() + '/Seed')"
# It will give the value but only of the first element
# and wont change with other items in the list.
# Have 2 other boxes with the same

app.mainloop()
The second combo box stays in one value upon following the second part. I do have a lot to learn, any one could tell me what the trouble is and how can i figure it out. please?

I'm Using Tkinter and if any other ui framework is there which can do this without this much issue. (Tk is nice but it looks very old.) then please point me the way to learn it.