A Novice
A Novice

Reputation: 1

Issue with Combobox

excuse the greeness. Trying to build GUI where option selected from Combobox populates text box. Nothing happening. First time programming so appreciate i have made a lot of errors here.

import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext

# function to display course selected
def courseDisplay():
    box = course.get() 
    print(box)

# Create instance
win = tk.Tk() 
win.resizable(130,130)   
win.title("RaceCourse GUI")

# create combobox
course = tk.StringVar()
courseChosen = ttk.Combobox(win,width=60,textvariable=course,state='readonly')   
courseChosen['values'] = ("Choose a course","Ascot", "Bath", "Chester")
courseChosen.grid(column=5, row=1,rowspan = 3, columnspan = 3,padx = 300, pady = 40)
courseChosen.current(0)
courseChosen.bind("<<ComboboxSelected>>", courseDisplay)

# create scrolled Text control    
scrolW  = 46
scrolH  =  10
box = scrolledtext.ScrolledText(win, width=scrolW, height=scrolH, wrap=tk.WORD)
box.grid(column=5, row=8, columnspan=3,padx = 300,pady = 10)

# Start GUI

win.mainloop()

Upvotes: 0

Views: 107

Answers (1)

Fejs
Fejs

Reputation: 2888

Since function courseDisplay is called when some event occurs on combobox (namely, when some option is selected), it should accept one variable (usually called event). So, your function should look like this:

def courseDisplay(event=None):
    box = course.get() 
    print(box)

Of course, You should add another logic for showing test in textbox instead of print.

Upvotes: 2

Related Questions