tkinter界面左边数据添加到右边

主要功能

tk创建的界面,左边是预设的数据,双击后将左边的数据添加到右边,包括全部添加按钮,全部删除按钮
已经添加过的不可重复添加,右边数据右键可以删除,也可以全部删除的功能,点击生成文档,将右边的数据生成列表
其中包含模糊搜索功能的,在搜索框里面输入关键字,会在左边对话框里面显示匹配的字段

不带模糊搜索对话框

import tkinter as tk
from tkinter import messagebox


class App:
    def __init__(self, master):
        self.master = master
        self.master.title('测试')
        self.master.geometry('600x700')

        # 左边框架
        self.left_frame = tk.Frame(self.master)
        self.left_frame.pack(side='left', padx=10, pady=10, fill='both', expand=True)

        # 左边列表框
        self.left_listbox = tk.Listbox(self.left_frame, width=30)
        self.left_listbox.pack(side='left', fill='both', expand=True)

        # 左边列表框添加滚动条
        self.left_scrollbar = tk.Scrollbar(self.left_frame, orient='vertical', command=self.left_listbox.yview)
        self.left_scrollbar.pack(side='right', fill='y')
        self.left_listbox.config(yscrollcommand=self.left_scrollbar.set)

        # 添加内容到左边列表框
        self.left_listbox.insert('end', '苹果')
        self.left_listbox.insert('end', '香蕉')
        self.left_listbox.insert('end', '橘子')

        # 左边列表框添加双击事件
        self.left_listbox.bind('<Double-Button-1>', self.add_item)

        # 中间框架
        self.middle_frame = tk.Frame(self.master)
        self.middle_frame.pack(side='left', padx=5, pady=5, fill='both', expand=True)

        # 全部添加按钮
        self.add_all_button = tk.Button(self.middle_frame, text='全部添加', command=self.add_all_items)
        self.add_all_button.pack(side='top', padx=5, pady=10)

        # 全部删除按钮
        self.delete_all_button = tk.Button(self.middle_frame, text='全部删除', command=self.delete_all_items)
        self.delete_all_button.pack(side='top', padx=5, pady=10)

        # 保存按钮
        self.save_button = tk.Button(self.middle_frame, text='生成文档', command=self.save_items)
        self.save_button.pack(side='top', padx=5, pady=10)

        # 右边框架
        self.right_frame = tk.Frame(self.master)
        self.right_frame.pack(side='left', padx=10, pady=10, fill='both', expand=True)

        # 右边列表框
        self.right_listbox = tk.Listbox(self.right_frame, width=30)
        self.right_listbox.pack(side='left', fill='both', expand=True)

        # 右边列表框添加滚动条
        self.right_scrollbar = tk.Scrollbar(self.right_frame, orient='vertical', command=self.right_listbox.yview)
        self.right_scrollbar.pack(side='right', fill='y')
        self.right_listbox.config(yscrollcommand=self.right_scrollbar.set)

        # 右键菜单
        self.popup_menu = tk.Menu(self.right_listbox, tearoff=0)
        self.popup_menu.add_command(label='删除', command=self.delete_item)

        # 右边列表框绑定右键事件
        self.right_listbox.bind('<Button-3>', self.show_popup_menu)

    def add_item(self, event=None):
        """添加单条数据到右边列表框"""
        if self.left_listbox.curselection():
            item = self.left_listbox.get(self.left_listbox.curselection())
            if item not in self.right_listbox.get(0, 'end'):
                self.right_listbox.insert('end', item)
            else:
                messagebox.showwarning('警告', '已添加过此内容!')
        else:
            messagebox.showerror('错误', '请选择要添加的内容!')

    def add_all_items(self):
        """添加全部数据到右边列表框"""
        for item in self.left_listbox.get(0, 'end'):
            if item not in self.right_listbox.get(0, 'end'):
                self.right_listbox.insert('end', item)

    def delete_item(self):
        """删除单条数据"""
        if self.right_listbox.curselection():
            self.right_listbox.delete(self.right_listbox.curselection())
        else:
            messagebox.showerror('错误', '请选择要删除的内容!')

    def delete_all_items(self):
        """删除全部数据"""
        self.right_listbox.delete(0, 'end')

    def show_popup_menu(self, event):
        """显示右键菜单"""
        self.popup_menu.post(event.x_root, event.y_root)

    def save_items(self):
        """保存数据"""
        items = self.right_listbox.get(0, 'end')
        if not items:
            messagebox.showwarning('警告', '数据为空,无需保存!')
        else:
            print(items)


root = tk.Tk()
app = App(root)
root.mainloop()

包含模糊搜索对话框

import tkinter as tk
from tkinter import messagebox
class App:
    def __init__(self, master):
        self.master = master
        self.master.title('测试')
        self.master.geometry('600x700')
        # 左边框架
        self.left_frame = tk.Frame(self.master)
        self.left_frame.pack(side='left', padx=10, pady=10, fill='both', expand=True)
        # 左边列表框
        self.left_listbox = tk.Listbox(self.left_frame, width=30)
        self.left_listbox.pack(side='top', fill='both', expand=True)
        # 左边列表框添加滚动条
        self.left_scrollbar = tk.Scrollbar(self.left_frame, orient='vertical', command=self.left_listbox.yview)
        self.left_scrollbar.pack(side='right', fill='y')
        self.left_listbox.config(yscrollcommand=self.left_scrollbar.set)
        # 添加内容到左边列表框
        self.left_listbox.insert('end', '苹果')
        self.left_listbox.insert('end', '香蕉')
        self.left_listbox.insert('end', '橘子')
        # 左边列表框添加双击事件
        self.left_listbox.bind('<Double-Button-1>', self.add_item)
        # 左边搜索框
        self.search_var = tk.StringVar(value='')
        self.search_entry = tk.Entry(self.left_frame, textvariable=self.search_var, width=20)
        self.search_entry.pack(side='bottom', pady=10)
        self.search_var.trace_add('write', self.search_items)
        # 中间框架
        self.middle_frame = tk.Frame(self.master)
        self.middle_frame.pack(side='left', padx=5, pady=5, fill='both', expand=True)
        # 全部添加按钮
        self.add_all_button = tk.Button(self.middle_frame, text='全部添加', command=self.add_all_items)
        self.add_all_button.pack(side='top', padx=5, pady=10)
        # 全部删除按钮
        self.delete_all_button = tk.Button(self.middle_frame, text='全部删除', command=self.delete_all_items)
        self.delete_all_button.pack(side='top', padx=5, pady=10)
        # 保存按钮
        self.save_button = tk.Button(self.middle_frame, text='生成文档', command=self.save_items)
        self.save_button.pack(side='top', padx=5, pady=10)
        # 右边框架
        self.right_frame = tk.Frame(self.master)
        self.right_frame.pack(side='left', padx=10, pady=10, fill='both', expand=True)
        # 右边列表框
        self.right_listbox = tk.Listbox(self.right_frame, width=30)
        self.right_listbox.pack(side='top', fill='both', expand=True)
        # 右边列表框添加滚动条
        self.right_scrollbar = tk.Scrollbar(self.right_frame, orient='vertical', command=self.right_listbox.yview)
        self.right_scrollbar.pack(side='right', fill='y')
        self.right_listbox.config(yscrollcommand=self.right_scrollbar.set)
        # 右键菜单
        self.popup_menu = tk.Menu(self.right_listbox, tearoff=0)
        self.popup_menu.add_command(label='删除', command=self.delete_item)
        # 右边列表框绑定右键事件
        self.right_listbox.bind('<Button-3>', self.show_popup_menu)
    def add_item(self, event=None):
        """添加单条数据到右边列表框"""
        if self.left_listbox.curselection():
            item = self.left_listbox.get(self.left_listbox.curselection())
            if item not in self.right_listbox.get(0, 'end'):
                self.right_listbox.insert('end', item)
            else:
                messagebox.showwarning('警告', '已添加过此内容!')
        else:
            messagebox.showerror('错误', '请选择要添加的内容!')
    def add_all_items(self):
        """添加全部数据到右边列表框"""
        for item in self.left_listbox.get(0, 'end'):
            if item not in self.right_listbox.get(0, 'end'):
                self.right_listbox.insert('end', item)
    def delete_item(self):
        """删除单条数据"""
        if self.right_listbox.curselection():
            self.right_listbox.delete(self.right_listbox.curselection())
        else:
            messagebox.showerror('错误', '请选择要删除的内容!')
    def delete_all_items(self):
        """删除全部数据"""
        self.right_listbox.delete(0, 'end')
    def show_popup_menu(self, event):
        """显示右键菜单"""
        self.popup_menu.post(event.x_root, event.y_root)
    def search_items(self, *args):
        """搜索左边列表框中包含搜索文本的数据"""
        search_text = self.search_var.get()
        self.left_listbox.delete(0, 'end')
        for item in ['苹果', '香蕉', '橘子']:
            if search_text in item:
                self.left_listbox.insert('end', item)
    def save_items(self):
        """保存数据"""
        items = self.right_listbox.get(0, 'end')
        if not items:
            messagebox.showwarning('警告', '数据为空,无需保存!')
        else:
            print(items)
root = tk.Tk()
app = App(root)
root.mainloop()

  目录