import tkinter as tk
class CourseSelectionApp:
def __init__(self, master):
self.master = master
self.master.title("Course SelectionApp")#标题
self.left_listbox = tk.Listbox(self.master, selectmode=tk.SINGLE)
self.right_listbox = tk.Listbox(self.master, selectmode=tk.SINGLE)
self.left_listbox.insert(0, "数据库")
self.left_listbox.insert(1, "计算机网络")
self.left_listbox.insert(2, "python")
self.left_listbox.insert(3, "大数据")
self.left_listbox.insert(4, "Android")
self.move_right_button = tk.Button(self.master, text=">", command=self.move_right)
self.move_left_button = tk.Button(self.master, text="<", command=self.move_left)
self.move_all_right_button = tk.Button(self.master, text=">>", command=self.move_all_right)
self.move_all_left_button = tk.Button(self.master, text="<<", command=self.move_all_left)
self.left_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.right_listbox.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
self.move_right_button.pack()
self.move_left_button.pack()
self.move_all_right_button.pack()
self.move_all_left_button.pack()
def move_right(self):
# Get the selected course from the left Listbox
selection = self.left_listbox.curselection()
if selection:
course = self.left_listbox.get(selection[0])
# Remove the course from the left Listbox and add it to the right Listbox
self.left_listbox.delete(selection[0])
self.right_listbox.insert(tk.END, course)
def move_left(self):
# Get the selected course from the right Listbox
selection = self.right_listbox.curselection()
if selection:
course = self.right_listbox.get(selection[0])
# Remove the course from the right Listbox and add it to the left Listbox
self.right_listbox.delete(selection[0])
self.left_listbox.insert(tk.END, course)
def move_all_right(self):
# Move all courses from the left Listbox to the right Listbox
for i in range(self.left_listbox.size()):
course = self.left_listbox.get(i)
self.right_listbox.insert(tk.END, course)
self.left_listbox.delete(0, tk.END)
def move_all_left(self):
# Move all courses from the right Listbox to the left Listbox
for i in range(self.right_listbox.size()):
course = self.right_listbox.get(i)
self.left_listbox.insert(tk.END, course)
self.right_listbox.delete(0, tk.END)
if __name__ == "__main__":
root = tk.Tk()
app = CourseSelectionApp(root)
root.mainloop()
【python】tk库实现简单选课系统
最新推荐文章于 2023-11-12 11:06:47 发布