1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
| from tkinter import * from tkinter import messagebox
window = Tk() window.geometry("500x250") window.resizable(0, 0)
frame1 = Frame(window) frame1.grid(row=0, column=0, sticky="nsew", padx=25)
frame2 = Frame(window) frame2.grid(row=0, column=1, sticky="nsew", padx=5, pady=50)
frame3 = Frame(window) frame3.grid(row=0, column=2, sticky="nsew", padx=5, pady=50)
frame4 = Frame(window) frame4.grid(row=0, column=3, sticky="nsew", padx=5, pady=50)
text = Text(frame1, width=25, height=6) text.grid(row=0, column=0, padx=10, pady=10)
listbox = Listbox(frame1, width=25, height=6) listbox.grid(row=1, column=0, padx=10, pady=10)
options = [ "Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6", "Option 7", "Option 8", "Option 9" ] for option in options: listbox.insert(END, option)
def clear_text(): if text.get("1.0", END).strip() == "": messagebox.showinfo("提示", "文本区域为空!") else: text.delete("1.0", END)
def activate(index): listbox.activate(index) listbox.selection_set(index) text.insert(INSERT, f"激活选项:{index + 1}\n")
def bbox(index): bbox_info = listbox.bbox(index) if bbox_info: text.insert(INSERT, f"选项{index + 1}的边框:{bbox_info}\n") else: text.insert(INSERT, f"选项{index + 1}无边框!\n")
def get_curselection(): selection = listbox.curselection() text.insert(INSERT, f"当前选项:{selection}\n")
def size(): size = listbox.size() text.insert(INSERT, f"选项总数:{size}\n")
def delete_options(index): listbox.delete(index, index + 1) text.insert(INSERT, f"删除选项:{index + 1}\n")
def get_options_text(index): options = listbox.get(index, index + 1) if options: text.insert( INSERT, f"选项{index + 1}到{index + 2}的文本:\n{options}\n" ) else: text.insert(INSERT, "没有找到范围内的文本!\n")
def get_index(index): idx = listbox.index(index + 1) text.insert(INSERT, f"返回选项{index + 1}的序号:{idx}\n")
def itemcget(index, option): value = listbox.itemcget(index, option) text.insert( INSERT, f"选项{index + 1}的{option}属性值:{value}\n" )
def itemconfig(index, **options): listbox.itemconfig(index, **options) text.insert(INSERT, f"选项{index + 1}标记为红色!\n")
def nearest_y(y): index = listbox.nearest(y) text.insert(INSERT, f"给定最近Y坐标{y}的选项:{index + 1}\n")
def selection_set(index): listbox.selection_set(index, index + 1) text.insert(INSERT, f"选中{index + 1}到{index + 2}\n")
def xview(command, *args): if command == "moveto": listbox.xview(*args) elif command == "scroll": listbox.xview(*args[0], *args[1:]) text.insert(INSERT, f"Xview command: {command} {args}\n")
def yview(command, *args): if command == "moveto": listbox.yview(*args) elif command == "scroll": listbox.yview(*args[0], *args[1:]) text.insert(INSERT, f"Yview command: {command} {args}\n")
def perform_action(action_type, index): if action_type == "activate": activate(listbox.curselection()[0]) elif action_type == "bbox": bbox(listbox.curselection()[0]) elif action_type == "get_curselection": get_curselection() elif action_type == "delete_options": delete_options(listbox.curselection()[0]) elif action_type == "get_options_text": get_options_text(listbox.curselection()[0]) elif action_type == "get_index": get_index(listbox.curselection()[0]) elif action_type == "itemcget": itemcget(listbox.curselection()[0], "background") elif action_type == "itemconfig": itemconfig(listbox.curselection()[0], background="red") elif action_type == "selection_set": selection_set(listbox.curselection()[0])
def on_button_click(action_type): selection = listbox.curselection() if selection: perform_action(action_type, selection[0]) else: messagebox.showwarning("提示", "请先选择一个选项!")
Button( frame2, text="清空内容", command=clear_text ).grid() Button( frame2, text="激活选项", command=lambda: on_button_click("activate") ).grid()
Button( frame2, text="获取边框", command=lambda: on_button_click("bbox") ).grid()
Button( frame2, text="当前选项", command=lambda: on_button_click("get_curselection") ).grid()
Button( frame3, text="选项总数", command=size ).grid()
Button( frame3, text="删除选项", command=lambda: on_button_click("delete_options") ).grid()
Button( frame3, text="获取文本", command=lambda: on_button_click("get_options_text") ).grid()
Button( frame3, text="选项序号", command=lambda: on_button_click("get_index") ).grid()
Button( frame4, text="选项属性", command=lambda: on_button_click("itemcget") ).grid()
Button( frame4, text="选项标记", command=lambda: on_button_click("itemconfig") ).grid()
Button( frame4, text="最近项目", command=lambda: nearest_y(10) ).grid()
Button( frame4, text="选择项目", command=lambda: on_button_click("selection_set") ).grid()
scrollbar = Scrollbar(frame1) scrollbar.grid(row=1, column=1, sticky="ns") listbox.config(yscrollcommand=scrollbar.set) scrollbar.config(command=listbox.yview)
window.mainloop()
|