I have looked on the internet but I can't move forward. Here it is, I have a combobox with a list of files' name that I display in the combobox. Once selected in the combobox, I would like to get the selected value. The function valeurList works, I have tried with other values. I have also tried with textvariable inside ttk.Combobox but without success. It returns something but it is blanck...Here is my code :
from tarfile import PAX_FIELDS
import tkinter as tk
from tkinter import CENTER, ttk
from turtle import bgcolor, width
from dataAcquire import listAcquire
from dataMeasure import Acquisitions
import dataAcquire
import dataMeasure
from numpy import pad, size
dataAcquire.listAcquire()
sensorList=["Température", "CO2", "CO", "Humidité", "Bruit", "Vibrations"]
def valeurList():
dataAcquire.recupName(current_value)
#create window
root = tk.Tk()
root.title("HarshSensor - Calcul de vos points de pénibilité")
root.geometry("950x600")
root.iconphoto(False, tk.PhotoImage(file='./image/icone.png'))
#create 2 frames
frame1 = tk.Frame(root, pady=30)
frame1.pack()
frame2 = tk.Frame(root)
frame2.pack()
#Label date et heure et boutton refresh
labelDH = tk.Label(frame1, text="Choisissez l'acquisition : ", padx=30)
labelDH.pack(side="left")
liste = ttk.Combobox(frame1, values=Acquisitions.listAcquisitions, state="readonly")
liste.pack(side="left", padx=20)
current_value=liste.get()
refresh = tk.Button(frame1, text="Refresh", command=valeurList)
refresh.pack(side="left")
CodePudding user response:
You need to pass textvariable
attribute of type tk.StringVar
to Combobox
constructor and this variable will get updated whenever you change selection in combobox.
CodePudding user response:
I think you need to change your state to "normal" in your combobox. This example should then grab your value, store it in the variable current_value and then print the value when you call valuerList from the button click.
from tarfile import PAX_FIELDS
import tkinter as tk
from tkinter import CENTER, ttk
from turtle import bgcolor, width
from dataAcquire import listAcquire
from dataMeasure import Acquisitions
import dataAcquire
import dataMeasure
from numpy import pad, size
dataAcquire.listAcquire()
sensorList=["Température", "CO2", "CO", "Humidité", "Bruit", "Vibrations"]
def valeurList():
current_value = liste.get() #this will get the information from the combobox
liste.delete("0", tk.END) # this will clear the field after button click
dataAcquire.recupName(current_value)
print(current_value)
#create window
root = tk.Tk()
root.title("HarshSensor - Calcul de vos points de pénibilité")
root.geometry("950x600")
root.iconphoto(False, tk.PhotoImage(file='./image/icone.png'))
#create 2 frames
frame1 = tk.Frame(root, pady=30)
frame1.pack()
frame2 = tk.Frame(root)
frame2.pack()
#Label date et heure et boutton refresh
labelDH = tk.Label(frame1, text="Choisissez l'acquisition : ", padx=30)
labelDH.pack(side="left")
liste = ttk.Combobox(frame1, values=Acquisitions.listAcquisitions,
textvariable=tk.StringVar(), state="normal")# changed your state to normal
liste.pack(side="left", padx=20)
refresh = tk.Button(frame1, text="Refresh", command=valeurList)
refresh.pack(side="left")