用户:Ta9yCTiD查看:0 回复:2 评论:0 创建时间:2023-06-10T19:21:25
【作品展示】

【作品介绍】
自己摸索,懒
【作品源代码】
import tkinter as tk
from tkinter import simpledialog, messagebox
import random
class RandomSelector:
def __init__(self, master):
self.master = master
master.title("随机选择器")
master.config(bg="#F1F1F1", padx=20, pady=20)
# 标题标签
title_label = tk.Label(
master, text="随机选择器", font=("Helvetica", 24, 'bold'), bg="#F1F1F1", fg="#333")
title_label.pack(pady=(20, 5))
# 列表框和滚动条
scrollbar = tk.Scrollbar(master)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.listbox = tk.Listbox(
master, font=("Helvetica", 12), bg="#F9F9F9", selectmode=tk.SINGLE, bd=0, yscrollcommand=scrollbar.set)
self.listbox.pack(pady=10, ipady=10, ipadx=20)
scrollbar.config(command=self.listbox.yview)
# 操作按钮
button_frame = tk.Frame(master, bg="#F1F1F1")
button_frame.pack(pady=10)
# 添加按钮
add_button = tk.Button(
button_frame, text="添加", font=("Helvetica", 12), bg="#5DA5A4", fg="#FFF", cursor="hand2",
activebackground="#42A48D", activeforeground="#F1F1F1", relief=tk.FLAT, command=self.add_item
)
add_button.pack(side=tk.LEFT, padx=10)
# 删除按钮
remove_button = tk.Button(
button_frame, text="删除", font=("Helvetica", 12), bg="#5DA5A4", fg="#FFF", cursor="hand2",
activebackground="#42A48D", activeforeground="#F1F1F1", relief=tk.FLAT, command=self.remove_item
)
remove_button.pack(side=tk.LEFT, padx=10)
# 编辑按钮
edit_button = tk.Button(
button_frame, text="编辑", font=("Helvetica", 12), bg="#5DA5A4", fg="#FFF", cursor="hand2",
activebackground="#42A48D", activeforeground="#F1F1F1", relief=tk.FLAT, command=self.edit_item
)
edit_button.pack(side=tk.LEFT, padx=10)
# 插入按钮
insert_button = tk.Button(
button_frame, text="插入", font=("Helvetica", 12), bg="#5DA5A4", fg="#FFF", cursor="hand2",
activebackground="#42A48D", activeforeground="#F1F1F1", relief=tk.FLAT, command=self.insert_item
)
insert_button.pack(side=tk.LEFT, padx=10)
# 选择按钮
choose_button = tk.Button(
button_frame, text="选择", font=("Helvetica", 12), bg="#5DA5A4", fg="#FFF", cursor="hand2",
activebackground="#42A48D", activeforeground="#F1F1F1", relief=tk.FLAT, command=self.choose_item
)
choose_button.pack(side=tk.LEFT, padx=10)
# 添加选项
def add_item(self):
item = simpledialog.askstring("添加", "请输入选项:")
if item:
self.listbox.insert(tk.END, item)
# 删除选项
def remove_item(self):
selection = self.listbox.curselection()
if not selection:
messagebox.showwarning("提示", "请选中一个选项再进行操作!")
else:
self.listbox.delete(selection)
# 编辑选项
def edit_item(self):
selection = self.listbox.curselection()
if not selection:
messagebox.showwarning("提示", "请选中一个选项再进行操作!")
else:
new_item = simpledialog.askstring(
"编辑", "请输入修改后的选项:", initialvalue=self.listbox.get(selection))
if new_item:
self.listbox.delete(selection)
self.listbox.insert(selection, new_item)
# 插入选项
def insert_item(self):
selection = self.listbox.curselection()
if not selection:
messagebox.showwarning("提示", "请选中一个选项再进行操作!")
else:
new_item = simpledialog.askstring("插入", "请输入新选项:")
if new_item:
self.listbox.insert(selection[0]+1, new_item)
# 随机选择
def choose_item(self):
if self.listbox.size() > 0:
random_selection = random.choice(self.listbox.get(0, tk.END))
messagebox.showinfo("选择结果", random_selection)
else:
messagebox.showwarning("提示", "当前没有选项!")
root = tk.Tk()
root.title("随机选择器")
root.geometry("500x400+300+200")
root.minsize(400, 300)
root.config(bd=10, bg="#F1F1F1")
app = RandomSelector(root)
app = RandomSelector(root)
root.mainloop()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn