我正在处理一个python3和tkinter项目。我想要它,所以当你双击一个列表框项目时,它会运行一个相应的文件。如:
doubleclick Notepad Clone>运行notepadclone.py,它位于给定的目录中。请帮忙!
#~~PyKit with ListBox View (test)~~#
from tkinter import *
import sys
import os
import webbrowser
import subprocess
from subprocess import run
from subprocess import call
appdir = "./apps"
master = Tk()
scrollbar = Scrollbar(master)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(master, yscrollcommand=scrollbar.set)
listbox.insert(END, "Double-Click an item to open it:")
for item in ["one", "two", "three", "four"]:
listbox.insert(END, item)
listbox.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=listbox.yview)
mainloop()
答案 0 :(得分:0)
您可以使用listbox.get('active')
获取用户双击的项目,然后使用字典将每个项目与要执行的功能相关联:
import tkinter as tk
def dbclick(event):
item = listbox.get('active') #get clicked item
dbclick_cmds[item]() # run associated command
appdir = "./apps"
master = tk.Tk()
scrollbar = tk.Scrollbar(master)
scrollbar.pack(side='right', fill='y')
listbox = tk.Listbox(master, yscrollcommand=scrollbar.set)
# dictionary that will contains the function associated to each item
dbclick_cmds = {}
listbox.insert('end', "Double-Click an item to open it:")
dbclick_cmds["Double-Click an item to open it:"] = lambda: None
for item in ["one", "two", "three", "four"]:
listbox.insert('end', item)
dbclick_cmds[item] = lambda i=item: print('run', i)
listbox.pack(side='left', fill='both')
scrollbar.config(command=listbox.yview)
listbox.bind('<Double-1>', dbclick)
master.mainloop()