首页 > 解决方案 > 使用带有列表框的单击事件在 python tkinter 中显示图像

问题描述

我正在尝试使用列表框创建单击事件,以便在从列表框中选择项目时在顶级窗口中显示图像文件。执行代码时,程序运行没有错误。但是,当我从列表框中选择一个项目时,我收到错误

“Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):
文件“C:\Users\steve\AppData\Local\Programs\Python\Python39\lib\tkinter_init _.py ”,第 1884 行,调用 返回 self.func (*args) TypeError: listboxevent() 接受 1 个位置参数,但给出了 2 个”。

如果我错了,请纠正我,但我不相信我将任何位置参数传递给 listboxevent() 方法。这里的任何见解将不胜感激。

from tkinter import *
import tkinter as tk

class Image_viewer:
    
    def __init__(self, win):
        self.root = win
        self.root.title('Root Window')
        self.root.geometry('200x150+600+200')
        
        self.lblHeading = tk.Label(self.root,
                                   text = 'Root Window',
                                   font = ('Times New Roman', 14),
                                   bg = 'White')
        self.lblHeading.pack(side = tk.TOP)
        self.lblHeading.focus()
        
        self.btnView = tk.Button(self.root, text = 'Image Viewer', command = self.view)
        self.btnView.place(relx = 0.5, rely = 0.9, anchor = tk.SE)
        self.btnClose = tk.Button(self.root, text = 'Close', command = self.close)
        self.btnClose.place(relx = 0.8, rely = 0.9, anchor = tk.SE)

   #function for inserting image based off selection
    def listboxinsert(self):
        self.imgas = tk.PhotoImage(file = '../raw/as.png')
        self.imggr = tk.PhotoImage(file = '../raw/gr.png')
        cs = self.Lb.curselection()
        
        if self.Lb.get(cs) == 0:
                #creates textbox to insert image into 
                self.mytext = tk.Text(self.top)
                self.mytext.place(relx = 0.5, rely = 0.80, height=150, width=200, anchor = tk.CENTER)
                #inserts image into textbox
                self.mytext.image_create(tk.END, image = self.imgas) 
        elif self.Lb.get(cs) == 1:
                #creates textbox to insert image into  
                self.mytext = tk.Text(self.top)
                self.mytext.place(relx = 0.5, rely = 0.80, height=150, width=200, anchor = tk.CENTER)
                #inserts image into textbox
                self.mytext.image_create(tk.END, image = self.imggr)
    def view(self):
        #creating top level
        self.top = Toplevel()
        self.top.title('Top Level')
        self.top.geometry('500x500')
        #Quit Button
        self.btnClose2 = Button(self.top, text="Quit", command= self.top.destroy)
        self.btnClose2.place(relx = 0.9, rely = 0.1, anchor = tk.SE)
        #initializing listbox
        self.Lb = Listbox(self.top, height=6) 
        self.Lb.place(relx = 0.3, rely = 0.3, anchor = tk.SE)
        #binding event to listbox selection
        self.Lb.bind('<<ListboxSelect>>', self.listboxinsert)

        # Inserting items in Listbox 
        self.Lb.insert(0, 'as') 
        self.Lb.insert(1, 'gr')
   
    def close(self):
        self.root.destroy()
      
def main():
    root = tk.Tk()
    Image_viewer(root)
    root.mainloop()
    
if __name__ == '__main__':
    main()

标签: pythontkinterlistboxbind

解决方案


推荐阅读