python - 使用带有列表框的单击事件在 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()
解决方案
推荐阅读
- angular - “错误:没有 Sugerir 的提供者!” 在 ionic 3 打字稿/角度应用程序中
- javascript - 在输入文本中使用粘贴时数据过滤器不起作用
- c# - 使用数组进行独特行程选择的最佳性能算法?
- c# - 如何获取内联 xml 架构?
- python - matplotlib.mathtext 函数返回 Unknown symbol: \begin{} 即使 usetex=True 和 \usepackage{amsmath} 正确配置
- python - 在 3.x 程序中有条件地执行部分 python 代码作为 2.x
- angularjs - 为 AngularJS 网站制作全屏加载图像
- linux - 无法安装 vmfs-tools
- java - 等待多个 RxJava observable 完成
- java - 为 Main 方法创建测试方法