-2

Working on a small project for a currency converter using Tkinter for a gui. I have the below code but it seems I have an error in combobox as it throws an error.

What am I missing?

Currency1_value=0
Entry1=Entry(window,textvariable=Currency1_value)
Entry1.grid(row=1,column=0)

CurrencyCombo=Combobox(window, state="readonly", values=("one", "two", "three"))
CurrencyCombo.grid(row=1,column=1)
M.Ustun
  • 289
  • 2
  • 6
  • 16
  • 2
    What is the error? Please provide [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Mike - SMT May 30 '18 at 15:00
  • The `textvariable=` option of any widget has to be one of the specific Tkinter variable types, such as `StringVar`. An ordinary Python variable is completely unusable here. – jasonharper May 30 '18 at 15:19
  • The error is; NameError: name 'Combobox' is not defined – M.Ustun May 30 '18 at 15:28
  • Combobox is part of **Ttk** so you'll have to `from tkinter import ttk` and use `CurrencyCombo = ttk.Combobox(...`. I'm posting an example below. – figbeam May 30 '18 at 15:54

1 Answers1

0
import tkinter as tk
from tkinter import ttk

root = tk.Tk()

combotext = tk.StringVar()
combotext.set('Select')

box = ttk.Combobox(root, textvariable=combotext, state='readonly')
box.pack()
box['values'] = ("Camembert",
                 "Brie",
                 "Tilsit",
                 "Stilton")

def callback_function(event):
    print('You selected:', combotext.get())

root.bind('<<ComboboxSelected>>', callback_function)

root.mainloop()
figbeam
  • 7,001
  • 2
  • 12
  • 18
  • Not sure if I'm doing this wrong but the selector is blank before you click on it and pick an option. – ArduinoBen Apr 12 '22 at 10:16
  • Hmmm. I'm getting the text "Select" in the box at first. Are you running the exact same code? – figbeam Apr 12 '22 at 15:07
  • Yeah I adapted the code for my own project. This thread (https://stackoverflow.com/questions/6876518/set-a-default-value-for-a-ttk-combobox) ended up giving me the answer. Since I had a function I needed to first put "global combotext" so that it remained in memory. – ArduinoBen Apr 19 '22 at 07:32