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

本文介绍如何在Tkinter中正确获取Entry输入信息和Listbox选中项。错误示范显示了直接使用get()可能导致的问题,原因是get()会在主循环结束时才返回值。解决方案是将get()绑定到特定事件,如回车键按下。同时,Listbox的选中信息可以通过curselection()获取,返回值为元组,需进一步处理以得到具体索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取Entry的输入信息

使用get()

错误示范

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 mainloop finishes, i.e. you close the tkinter application.

在mainloop 结束后时打印才起作用

因此如果希望在运行程序时就调用Entry输入的内容,需要把get()同某一事件绑定

If you want to call get() when some text is entered (you probably do), you need to bind get() to an event.

If you want to print the contents of your Entry widget while your program is running, you need to schedule a callback. For example, you can listen to the pressing of the Return key as follows

通过回车键绑定callbackEntry的内容

import tkinter as tk
def on_change(e):
	#打印输入信息
    print(e.widget.get())

root = tk.Tk()
e = tk.Entry(root)
e.pack()    
# Calling on_change when you press the return key
# get()必须绑定在一个事件中,这里使用的回车键
e.bind("<Return>", on_change)  
root.mainloop()

解决方案来自stackoverflow

获取Listbox选中的信息

  • 使用Listbox.curselection()获取所选项的 索引
  • 以tupple形式返回,如果需要获取具体索引值,需访问tupple
  • 同get()方法一眼,如果需要在程序运行过程中打印所选择的信息,需要将Listbox.curselection()同事件绑定

示例:

import tkinter as tk
def language_set(lang_set):
    langs=['chi_sim','eng','fra','deu']
    select_index=lang_set.widget.curselection()
    #返回的是tupple,需要调用tupple的值
    lang_index=select_index[0]
    print("idex numebr {0}".format(select_index))
    
myWindow = tk.Tk()
myWindow.title('tkinter-Listbox')
myWindow.geometry('500x300')

lang_text=tk.StringVar()
lang_text.set(('简体中文','英语','法语','德语'))
lang_set = tk.Listbox(myWindow, selectmode=tk.BROWSE,listvariable=lang_text)  
lang_set.pack(side='top')
lang_set.bind("<Return>", language_set) 

myWindow.mainloop()
以下是使用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、付费专栏及课程。

余额充值