获取组合框值 python

Get combobox value python

我在网上看过,但我无法前进。在这里,我有一个组合框,其中包含我在组合框中显示的文件名列表。在组合框中选择后,我想获取选定的值。函数 valeurList 有效,我尝试使用其他值。我也尝试过在 ttk.Combobox 中使用 textvariable 但没有成功。它 returns 有点东西,但它是空白的...这是我的代码:


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")

您需要将 tk.StringVar 类型的 textvariable 属性传递给 Combobox 构造函数,只要您在组合框中更改选择,此变量就会更新。

我认为您需要将组合框中的状态更改为“正常”。然后,此示例应获取您的值,将其存储在变量 current_value 中,然后在您从单击按钮调用 valuerList 时打印该值。

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")