Python Forum

Full Version: [python] [Tkinter] Problem bidding combobox with np.array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to include combobox values into np.array taken from function but it does not work. Could you please help me to make it workable. I will appreciate your help.

import tkinter as tk
from tkinter.ttk import *


import numpy as np
import pandas as pd

from scipy.stats import norm


master = tk.Tk()

v = tk.IntVar()
combo = Combobox(master)




def callback(event):
    a = [float(w1.get()),float(w2.get()),float(w3.get()),float(w4.get())]
   
    print(a)
   


w1 = Combobox(master)
w1['values']= (0.2,0.3,0.4,0.1)
w1.current(0) #set the selected item
w1.grid(row=3, column=2)



w2= Combobox(master)
w2['values']= (0.2,0.3,0.4,0.1)
w2.current(0) #set the selected item
w2.grid(row=4, column=2)



w3= Combobox(master)
w3['values']= (0.2,0.3,0.4,0.1)
w3.current(0) #set the selected item
w3.grid(row=5, column=2)



w4= Combobox(master)
w4['values']= (0.2,0.3,0.4,0.1)
w4.current(0) #set the selected item
w4.grid(row=6, column=2)
w4.bind("<<ComboboxSelected>>", callback)



weights = np.array([a])


master.mainloop()
In weights = np.array([a]) a has not been defined

There is an a local to the function callback this a wont have any values until w4 Combobox has been selected which will call the function callback and is only accessible in that function.
(Aug-04-2019, 10:50 AM)Yoriz Wrote: [ -> ]In weights = np.array([a]) a has not been defined There is an a local to the function callback this a wont have any values until w4 Combobox has been selected which will call the function callback and is only accessible in that function.


So what to do ? I tried to make a global but instead it gives me random values from the values. Could you please help?
It depends on what you are trying to do with
weights = np.array([a])
If you moved it to the function it would be created when the callback happens

def callback(event):
    a = [float(w1.get()),float(w2.get()),float(w3.get()),float(w4.get())]
    
    print(a)
    weights = np.array([a])
but you would not be able to access weights outside of the call back.

You would have a better time using classes for GUI code.