python - 将选择列表框顶部窗口传递到主窗口
问题描述
这是我的第一个真正的 Python 项目。我目前正在 Tkinter 中开发一个 GUI,它允许用户选择任务和 CV,以使用来自数据库的标准预定义任务和 CV 文本自动编译文档。
我在主窗口中创建了两个“添加”按钮来添加任务和 CV,它们显示一个弹出列表框,允许用户选择他们想要包含在商业提案中的任务和 CV。我设法将弹出窗口创建为一个单独的类,并将选定的任务存储在一个列表中,但是现在当用户单击选择按钮时,我需要将带有所选项目的列表传递给主窗口中的列表框弹出窗口,但我无法理解如何做到这一点。
我研究了不同的论坛并观看了各种 Youtube 视频,但都专注于条目弹出窗口或某种形式。
这是主窗口的代码:
from tkinter import *
from Add import *
# make main window
root = Tk()
theLabel = Label(root, text="ProposalBuilder")
theLabel.grid(row=0)
# make frames
taskFrame = Frame(root)
taskFrame.grid(row=1, column=0)
CVFrame = Frame(root)
CVFrame.grid(row=1, column=1)
buildFrame = Frame(root)
buildFrame.grid(row=2, columnspan=2)
# add labels to frames
taskLabel = Label(taskFrame, text="Tasks")
taskLabel.pack()
CVLabel = Label(CVFrame, text="CVs")
CVLabel.pack()
# add listboxes to frames
scrollTask = Scrollbar(taskFrame, orient=VERTICAL)
listTask = Listbox(taskFrame, selectmode=MULTIPLE, yscrollcommand=scrollTask.set)
scrollTask.config(command=listTask.yview)
scrollTask.pack(side=RIGHT, fill=Y)
listTask.pack()
scrollCV = Scrollbar(CVFrame, orient=VERTICAL)
listCV = Listbox(CVFrame, selectmode=MULTIPLE, yscrollcommand=scrollCV.set)
scrollCV.config(command=listCV.yview)
scrollCV.pack(side=RIGHT, fill=Y)
listCV.pack()
# add commands to buttons
def addTask():
taskBox = Add('C:\\Users\\204703\\ProposalBuilder\\Database')
sel_test = taskBox.selection
def addCV():
CVBox = Add('C:\\Users\\204703\\ProposalBuilder\\Database')
# add buttons to frames
buttonAddTask = Button(taskFrame, text="Add", command=addTask)
buttonAddTask.pack(fill=X)
buttonDelTask = Button(taskFrame, text="Delete")
buttonDelTask.pack(fill=X)
buttonUpTask = Button(taskFrame, text="Up")
buttonUpTask.pack(fill=X)
buttonDownTask = Button(taskFrame, text="Down")
buttonDownTask.pack(fill=X)
buttonAddCV = Button(CVFrame, text="Add", command=addCV)
buttonAddCV.pack(fill=X)
buttonDelCV = Button(CVFrame, text="Delete")
buttonDelCV.pack(fill=X)
buttonUpCV = Button(CVFrame, text="Up")
buttonUpCV.pack(fill=X)
buttonDownCV = Button(CVFrame, text="Down")
buttonDownCV.pack(fill=X)
buttonBuild = Button(buildFrame, text="Build Proposal")
buttonBuild.pack(side=RIGHT)
root.mainloop()
这是我为弹出窗口创建的单独类的代码:
from tkinter import*
from os import *
class Add:
def __init__(self, path):
# the slected tasks
self.selection = []
# make a frame
top = Toplevel()
# get file names from the directory (path) and save in list
self.path = path
self.dirList = listdir(self.path)
# add listbox to frames and populate with file names
self.scrollList = Scrollbar(top, orient=VERTICAL)
self.listbox = Listbox(top, selectmode=MULTIPLE, yscrollcommand=self.scrollList.set)
self.scrollList.config(command=self.listbox.yview)
self.scrollList.pack(side=RIGHT, fill=Y)
for item in self.dirList:
self.listbox.insert(END,item)
self.listbox.pack()
# add buttons to frame
self.selectButton = Button(top, text="Select", command=self.select)
self.selectButton.pack()
self.quitButton = Button(top, text="Quit", command=top.destroy)
self.quitButton.pack()
# identify selected rows and return a list with the selection
def select(self):
selectedRows = self.listbox.curselection()
for item in selectedRows:
self.selection.append(self.dirList[item])
print(self.selection)
return self.selection
解决方案
问题:将带有所选项目的列表传递到主窗口中的列表框
您需要主窗口列表框的引用。
我展示,如何使用listTask
扩展您
__init__
以接受参考target_listbox
class Add: def __init__(self, target_listbox, path): self.target_listbox = target_listbox
将所选项目插入
.target_listbox
注意:您的,return self.selection
没用的,Button.command
无法处理退货。def select(self): selectedRows = self.listbox.curselection() for item in selectedRows: self.target_listbox.insert(tk.END, self.dirList[item])
将引用传递
listTask
给Add(...
taskBox = Add(listTask, ...)
推荐阅读
- java - Java 静态方法、通配符/通用返回类型
- python-3.x - 以管理员身份运行的 Visual Studio 代码不会运行代码
- c - Linux timer_setup 函数
- linux - 将 libv8-3.14 的外部库实例安装到文件夹
- python-3.x - 查询 S3 存储桶文件日期的 Boto3 脚本速度很慢
- python - 调用随机方法时,MagicMock 如何避免抛出 AttributeError?
- c - 当我尝试重新创建管道/分叉一个新进程时,进程卡住了
- excel - Excel VBA:将单元格中的分隔字符串与值列进行比较
- java - 使用 KafkaListenerEndpointRegistry 注册 kafkalistenerContainers
- sql-server - 为什么数据库和 SQL 查询使用相同的排序规则问题?