tkinter-获取输入的信息-Entry/Listbox

错误示范:from tkinter import *root = Tk()e= Entry(root)e.pack()entry = e.get()root.mainloop()print("Entered text:"+entry)返回:Entered text:问题:使用get()时返回空值原因:Your eventual second problem is that the text would anyhow be printed only after the main
摘要由CSDN通过智能技术生成

获取Entry的输入信息

使用get()

错误示范

from tkinter import *
root = Tk()
e= Entry(root)
e.pack()
entry = e.get()
root.mainloop()
print("Entered text:"+entry)

返回:

Entered te
  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Tkinter Listbox进行修改的示例代码: ```python import tkinter as tk class ListBoxDemo(tk.Frame): def __init__(self, master=None): super().__init__(master) self.pack() self.create_widgets() def create_widgets(self): # 创建Listbox self.listbox = tk.Listbox(self) self.listbox.pack(side="left", fill="both") # 添加数据 for item in ["apple", "banana", "cherry"]: self.listbox.insert("end", item) # 创建修改按钮 self.modify_button = tk.Button(self, text="Modify", command=self.modify_item) self.modify_button.pack(side="top") def modify_item(self): # 获取当前选中的项 selected_item = self.listbox.curselection() if selected_item: # 获取选中项的索引 index = selected_item[0] # 获取选中项的值 value = self.listbox.get(index) # 创建修改窗口 self.modify_window = tk.Toplevel(self) self.modify_window.title("Modify Item") # 创建标签和输入框 self.label = tk.Label(self.modify_window, text="New Value:") self.label.pack(side="left") self.entry = tk.Entry(self.modify_window) self.entry.pack(side="left") # 设置输入框的默认值为选中项的值 self.entry.insert(0, value) # 创建确认按钮 self.confirm_button = tk.Button(self.modify_window, text="Confirm", command=self.confirm_modify) self.confirm_button.pack(side="left") def confirm_modify(self): # 获取输入框的值 new_value = self.entry.get() # 获取当前选中的项 selected_item = self.listbox.curselection() if selected_item: # 获取选中项的索引 index = selected_item[0] # 修改选中项的值 self.listbox.delete(index) self.listbox.insert(index, new_value) # 关闭修改窗口 self.modify_window.destroy() root = tk.Tk() app = ListBoxDemo(master=root) app.mainloop() ``` 运行上述代码后,会弹出一个窗口,其中包含一个Listbox和一个Modify按钮。点击Modify按钮后,会弹出一个新窗口,其中包含一个标签和一个输入框,用于修改Listbox中选中项的值。修改完成后,点击确认按钮即可完成修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值