Python Forum
.get() not retrieving value?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.get() not retrieving value?
#1
Hi Everyone,

I have setup a combo box to store gradient values as such:
    gradient_value = ["0%","10%","15%","20%"]
    gradient_dropdown = ttk.Combobox(column_frame, values=gradient_value, state="readonly")
    gradient_dropdown.pack(pady=5)
I am trying to retrieve that selected value when I select it in my gui using the drop down box using:
stored_value = None
def store_selected_value(event=None):
    grad_sel = gradient_dropdown.get()
    global stored_value
    stored_value= grad_sel
    print(f"Gradient: '{stored_value}'")
I have binded as such:
    gradient_dropdown.bind("<<ComboboxSelected>>", store_selected_value)
which is tacked on the end of my combo creating code:
    gradient_value = ["0%","10%","15%","20%"]
    gradient_dropdown = ttk.Combobox(column_frame, values=gradient_value, state="readonly")
    gradient_dropdown.pack(pady=5)
    gradient_dropdown.bind("<<ComboboxSelected>>", store_selected_value)
however when i select different options in my gui for gradient, it prints empty ''
can someone please help me how to store the value so i can reference as I am trying in the stored_value variable?
Gribouillis write Aug-25-2023, 06:37 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Here is an example using a callback function

import tkinter as tk
from tkinter import ttk

# Function to print value. Using combo.get() to get the value
def callback(event):
    label['text'] = f'Selected: {combo.get()}'

root = tk.Tk()
root.geometry('400x200+300+300')

# Label to display text
label = tk.Label(root, anchor='w')
label.pack(fill='x',padx=10)

# Some values to populate the combobox
values = [(f'Value {i}') for i in range(11)]

# Define the combobox
combo = ttk.Combobox(root, state='readonly')
combo['values'] = values
combo.current(0)
combo.pack(pady=5)

# Bind the combobox and set the function callback
combo.bind('<<ComboboxSelected>>', callback)

# Print starting value for label
label['text'] = callback(event=None)

root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Doing my best to reconstruct your code I came up with this:
import tkinter as tk
import tkinter.ttk as ttk


def store_selected_value(event=None):
    grad_sel = gradient_dropdown.get()
    global stored_value
    stored_value= grad_sel
    print(f"Gradient: '{stored_value}'")


root = tk.Tk()
stored_value = None
column_frame = tk.Frame(root)
column_frame.pack(padx=50, pady=50)

gradient_value = ["0%","10%","15%","20%"]
gradient_dropdown = ttk.Combobox(column_frame, values=gradient_value, state="readonly")
gradient_dropdown.pack(pady=5)
gradient_dropdown.bind("<<ComboboxSelected>>", store_selected_value)

root.mainloop()
As I select the gradient values one at a time it prints:
Output:
Gradient: '0%' Gradient: '10%' Gradient: '15%' Gradient: '20%'
The problem must be somewhere else in your code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  KeyError while retrieving ESPN data john317ab 2 828 Nov-29-2023, 09:07 PM
Last Post: john317ab
  [Solved] Retrieving a pdf from sqlite3 BigMan 4 2,341 Mar-12-2022, 01:56 PM
Last Post: deanhystad
  Retrieving a column from a data set using a function Bayle 6 2,361 Oct-06-2021, 08:52 PM
Last Post: Bayle
  Retrieving Cookies whois1230 2 2,189 Nov-21-2020, 12:01 PM
Last Post: snippsat
  Problem: Retrieving Form data PythonDev 3 3,117 Oct-16-2020, 02:09 AM
Last Post: PythonDev
  Retrieving dictionary keys within with another dictionay bazcurtis 8 2,857 Oct-29-2019, 10:06 PM
Last Post: bazcurtis
  Retrieving items from JSON bazcurtis 12 5,078 Oct-27-2019, 05:18 PM
Last Post: bazcurtis
  Trouble retrieving dictionary from mysql.connector cursor swechsler 2 3,067 Sep-17-2019, 05:21 PM
Last Post: swechsler
  retrieving pvalue from statsmodels results Staph 4 3,040 Jul-18-2019, 03:27 PM
Last Post: Gribouillis
  PRAW and PyQt: Immense slowdown when retrieving Reddit posts and adding them to GUI codebro 2 3,234 Dec-30-2018, 01:19 AM
Last Post: codebro

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020