[英]Get user input from tk.Text and tk.Listbox without using tk.Button
我在 Python 中使用tkinter
庫制作了一個 UI。 不幸的是,我不知道如何在不使用按鈕的情況下從tk.Text
或tk.Listbox
小部件獲取用戶輸入。 我試圖將用戶輸入分配給一個變量,該變量在使用按鈕調用root.destroy
或手動關閉 UI 后立即采用永久值。 這適用於tk.Entry
或tk.Radiobutton
等小部件。 我想知道我怎樣才能讓它也適用於tk.Text
和tk.Listbox
小部件。 下面顯示了一個示例代碼,希望它能闡明我正在嘗試做的事情。
# Import tkinter library
import tkinter as tk
import tkinter.ttk as ttk
# Function to create root
def CreateRoot(Pos, Dim, Color, Text):
Root = tk.Tk()
Root.config(width = Pos[0])
Root.config(height = Pos[1])
Root.geometry(str(Dim[0]) + 'x' + str(Dim[1]))
Root.config(bg = Color)
Root.title(Text)
return Root
# Function to add entry to root
# that allows one element
# from some given list 'Val'
def CombEntry(Root, Pos, Dim, Style, Var, Val):
Entry = ttk.Combobox(Root)
Entry.place(x = 1000 * Pos[0])
Entry.place(y = 1000 * Pos[1])
Entry.place(width = 1000 * Dim[0])
Entry.place(height = 1000 * Dim[1])
Entry.config(justify = Style[0])
Entry.config(state = Style[1])
Entry.config(font = Style[2])
Entry.config(textvariable = Var)
Entry.config(values = Val)
return Entry
# Function to add entry to root
# that allows one or more elements
# from some given list 'Val'
def ListEntry(Root, Pos, Dim, Style, Var, Val):
Entry = tk.Listbox(Root)
Entry.place(x = 1000 * Pos[0])
Entry.place(y = 1000 * Pos[1])
Entry.place(width = 1000 * Dim[0])
Entry.place(height = 1000 * Dim[1])
Entry.config(justify = Style[0])
Entry.config(state = Style[1])
Entry.config(font = Style[2])
Entry.config(selectmode = 'multiple')
Entry.config(listvariable = Var)
Entry.insert(tk.END, *Val)
return Entry
# Function to add entry to root
# that allows one line or more
# without further restrictions
def TextEntry(Root, Pos, Dim, Style, Var):
Entry = tk.Text(Root)
Entry.place(x = 1000 * Pos[0])
Entry.place(y = 1000 * Pos[1])
Entry.place(width = 1000 * Dim[0])
Entry.place(height = 1000 * Dim[1])
#Entry.config(justify = Style[0]) # Non-existent attribute
Entry.config(state = Style[1])
Entry.config(font = Style[2])
#Entry.config(variable = Var) # Non-existent attribute
return Entry
# Create root and add entries
dims = [0.200,0.050]
style = ['l','normal','Calibri 12']
root = CreateRoot([0,0], [500,500], '#DDDDDD', 'Title')
tkvars = [tk.StringVar(), tk.StringVar(), tk.StringVar()]
entry2 = CombEntry(root, [0.025,0.025], dims, style, tkvars[0], ['A','B','C'])
entry3 = ListEntry(root, [0.025,0.100], dims, style, tkvars[1], ['A','B','C'])
entry1 = TextEntry(root, [0.025,0.175], dims, style, tkvars[2])
root.mainloop()
# Print user input without having used
# a tk.button with command
user_input = [i.get() for i in tkvars]
print('user_input = {}'.format(user_input))
print('\nnotice how user_input[0] is correct..')
print('but user_input[1] and user_input[2] are incorrect')
如果您只想在小部件中輸入最后的值,則可以使用根 window 的協議處理程序。
# Import tkinter library
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.messagebox import askyesno
...
entry1 = TextEntry(root, [0.025,0.175], dims, style, tkvars[2])
# works when GUI is closed manually, close button [x], Alt + F4
# protocol handler
def close():
# creating global variables in a function
global combobox_value, listbox_value, text_value
combobox_value = entry2.get()
# entry3.curselection() is a tuple with the indices of the selected list items
listbox_value = [entry3.get(i) for i in entry3.curselection()]
text_value = entry1.get("1.0", "end-1c")
# optional, validation of values
if not all([combobox_value, listbox_value, text_value]):
answer = askyesno(title="Info", message="Not all values have been selected. " \
"Close the application anyway?")
if not answer: # False
return
root.destroy()
# exit with saving data
root.protocol("WM_DELETE_WINDOW", close)
root.mainloop()
# Print user input without having used
# a tk.button with command
print("User input:", combobox_value, listbox_value, text_value)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.