items = ["Item 1", "Item 2", "Item 3", "Item 4"] for item in items: listbox.insert(tk.END, item) 四、绑定事件 使用Tkinter的bind方法将Listbox事件绑定到特定的处理函数。这样,当用户选择Listbox中的某个项目时,就会触发<<ListboxSelect>>事件,并调用
这段代码创建了一个Tkinter窗口,其中包含一个Listbox组件。当用户点击Listbox中的项目时,会触发on_listbox_click函数,并在控制台打印出选中的项目。
创建主窗口:我们首先创建一个ScrollableList类,继承自tk.Tk,设置窗口标题和大小。 添加列表框:通过Listbox组件创建一个可以显示多个项目的列表。 添加滚动条:滚动条与列表框相连接,便于用户在长列表中滚动。 事件绑定:使用<Button-1>事件,这是一个鼠标左键点击事件。当用户点击某一项时,触发on_item_click方法。 滚...
sshow=Scrollbar(master)sshow.pack(side=RIGHT,fill=Y)lbshow1=Listbox(master,fg="red",height=5,width=20)# 创建需要滚动条的列表框 lbshow1["yscrollcommand"]=sshow.set#把滚动条对象赋值给列表框属性 lbshow1.pack(side="right")# 设置滚动条在右边foriteminrange(10):lbshow1.insert(END,item)...
button = tk.Button(root, text="Click me!", command=on_click) button.pack() root.mainloop() 在这个例子中,我们定义了一个名为on_click的函数,当用户点击按钮时,会打印出 "Button clicked!"。 六、输入框(Entry) 输入框允许用户输入文本。你可以获取输入框中的文本并进行处理。
listbox.bind("<<ListboxSelect>>", on_select) I have executed the above example code and added the screenshot below. In this example, we define a function on_select that is triggered whenever an item is selected in the list box. We retrieve the selected indicescurselection()and then get...
def append_record(date, item, amount): with open("ledger.txt", " ① ") as file: file.write(f"{date},{item},{amount}\n")def read_records(): with open(" ② ", "r") as file: records = file. ③ () for record in records: print(record, ...
showClick)self.menu.add_command(label="退出",command=self.onExit)self.master.bind("<Button-3>",self.showMenu)# 窗体鼠标右键函数,调用showMenu函数self.pack()def showMenu(self,e):self.menu.post(e.x_root,e.y_root)def showClick(self):tkinter.messagebox.showinfo("提示","点上了")def on...
btn.Bind(wx.EVT_BUTTON, lambda e, mark=i: self.on_click(e, mark)) box.Add(btn, 0, wx.LEFT) panel.SetSizer(box) def on_click(self, event, mark): wx.MessageDialog(self, 'click mark:{}'.format( mark), 'click btn', wx.ICON_INFORMATION).ShowModal() ...
import tkinter as tk root = tk.Tk() listbox = tk.Listbox(root, justify=tk.CENTER) listbox.pack() listbox.insert(tk.END, "Item 1") listbox.insert(tk.END, "Item 2") listbox.insert(tk.END, "Item 3") root.mainloop() 在上述示例中,我们创建了一个列表框,并将其对齐方式设置为居中对...