不,我要能实现用Python的tkinter写一个列表的输入界面,并要求可以添加或删除列表数据,且可以拖动列表元素位置,并有保存按钮,可以保存至TXT文件的代码
时间: 2024-05-07 21:16:35 浏览: 80
当然可以!以下是一个简单的实现示例:
```python
import tkinter as tk
class ListInput(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.inputs = []
self.create_widgets()
def create_widgets(self):
self.listbox = tk.Listbox(self, width=50)
self.listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar = tk.Scrollbar(self, orient=tk.VERTICAL)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=self.listbox.yview)
self.entry = tk.Entry(self, width=50)
self.entry.pack(side=tk.TOP)
add_button = tk.Button(self, text="Add", command=self.add)
add_button.pack(side=tk.TOP, pady=5)
delete_button = tk.Button(self, text="Delete", command=self.delete)
delete_button.pack(side=tk.TOP)
save_button = tk.Button(self, text="Save", command=self.save)
save_button.pack(side=tk.BOTTOM, pady=5)
def add(self):
self.inputs.append(self.entry.get())
self.listbox.insert(tk.END, self.entry.get())
self.entry.delete(0, tk.END)
def delete(self):
selection = self.listbox.curselection()
if selection:
self.listbox.delete(selection)
self.inputs.pop(selection[0])
def save(self):
with open("data.txt", "w") as f:
f.write("\n".join(self.inputs))
root = tk.Tk()
root.title("List Input")
app = ListInput(root)
app.pack(fill=tk.BOTH, expand=True)
root.mainloop()
```
注意,本AI并不能保证代码的正确性和完整性,仅供参考和学习使用,具体实现还需根据你的具体需求来修改和完善。
阅读全文