I am attempting to bind a selection event to items within a combobox to display an image inside a toplevel window once the specified item is selected. The code I have runs without error. However, the images are not displayed upon selecting the items inside the combobox. Any help would be appreciated.
JavaScript
x
65
65
1
from tkinter import *
2
import tkinter as tk
3
from tkinter import ttk
4
5
class Image_viewer:
6
7
def __init__(self, win):
8
self.root = win
9
self.root.title('Root Window')
10
self.root.geometry('200x150+600+200')
11
12
self.lblHeading = tk.Label(self.root,
13
text = 'Root Window',
14
font = ('Times New Roman', 14),
15
bg = 'White')
16
self.lblHeading.pack(side = tk.TOP)
17
self.lblHeading.focus()
18
19
self.btnView = tk.Button(self.root, text = 'Image Viewer', command = self.view)
20
self.btnView.place(relx = 0.5, rely = 0.9, anchor = tk.SE)
21
self.btnClose = tk.Button(self.root, text = 'Close', command = self.close)
22
self.btnClose.place(relx = 0.8, rely = 0.9, anchor = tk.SE)
23
def CMBinsert(self):
24
self.imgas = tk.PhotoImage(file = '../raw/as.png')
25
self.imggr = tk.PhotoImage(file = '../raw/gr.png')
26
if self.box_value.get() == 'as':
27
#creates textbox to insert image into
28
self.mytext = tk.Text(self.top)
29
self.mytext.place(relx = 0.5, rely = 0.80, height=150, width=200, anchor = tk.CENTER)
30
#inserts image into textbox
31
self.mytext.image_create(tk.END, image = self.imgas)
32
elif self.box_value.get() == 'gr':
33
#creates textbox to insert image into
34
self.mytext = tk.Text(self.top)
35
self.mytext.place(relx = 0.5, rely = 0.80, height=150, width=200, anchor = tk.CENTER)
36
#inserts image into textbox
37
self.mytext.image_create(tk.END, image = self.imgas)
38
else:
39
pass
40
def view(self):
41
#creating top level
42
self.top = Toplevel()
43
self.top.title('Top Level')
44
self.top.geometry('500x500')
45
#Quit Button
46
self.btnClose2 = Button(self.top, text="Quit", command= self.top.destroy)
47
self.btnClose2.place(relx = 0.9, rely = 0.1, anchor = tk.SE)
48
#initializing combobox
49
self.box_value=StringVar()
50
self.CMB = ttk.Combobox(self.top, textvariable=self.box_value, state='readonly')
51
self.CMB['values']=['as','gr']
52
self.CMB.place(relx = 0.6, rely = 0.3, anchor = tk.SE)
53
#binding selection event to combobox selection event
54
self.CMB.bind("<<ComboboxSelected>>", self.CMBinsert())
55
def close(self):
56
self.root.destroy()
57
58
def main():
59
root = tk.Tk()
60
Image_viewer(root)
61
root.mainloop()
62
63
if __name__ == '__main__':
64
main()
65
Advertisement
Answer
This is a subtle issue with binding the callback to the Combobox event, you need to pass in a function as an argument, but you wrote
JavaScript
1
2
1
self.CMB.bind("<<ComboboxSelected>>", self.CMBinsert())
2
with the parenthesis after self.CMBinsert
, so you’re not passing the function, you’re actually calling the function once, and passing in the result instead. Just remove those parenthesis:
JavaScript
1
2
1
self.CMB.bind("<<ComboboxSelected>>", self.CMBinsert)
2
After that, you get an error on the callback’s signature:
JavaScript
1
2
1
TypeError: CMBinsert() takes 1 positional argument but 2 were given
2
because it’s expecting an event
parameter, just change your def to:
JavaScript
1
2
1
def CMBinsert(self, evt):
2