Python ¿;如何捕获在文本框中输入的值并将其保存在变量中,以便在程序的其他部分中使用?
我希望我在inputValue变量中得到的值可以用于代码的另一部分,但是当窗口被破坏时,输入的值就会丢失Python ¿;如何捕获在文本框中输入的值并将其保存在变量中,以便在程序的其他部分中使用?,python,tkinter,Python,Tkinter,我希望我在inputValue变量中得到的值可以用于代码的另一部分,但是当窗口被破坏时,输入的值就会丢失 from tkinter import * root=Tk() def retrieve_input(): inputValue=textBox.get("1.0","end-1c") print(inputValue) def Close(): root.quit() def Ambas(): retrieve_input() Close()
from tkinter import *
root=Tk()
def retrieve_input():
inputValue=textBox.get("1.0","end-1c")
print(inputValue)
def Close():
root.quit()
def Ambas():
retrieve_input()
Close()
textBox=Text(root, height=2, width=10)
textBox.pack()
buttonCommit=Button(root, height=1, width=10, text="Aceptar", command=lambda: Ambas())
#command=lambda: retrieve_input() >>> just means do this when i press the button
buttonCommit.pack()
mainloop()
在函数中创建的变量是局部变量-必须使用
全局
将值分配给全局变量
#from tkinter import * # PEP8: `import *` is not preferred
import tkinter as tk
# --- functions ---
def ambas(): # PEP8: `lower_case_names` for functions and variables
global input_value
input_value = text_box.get("1.0", "end-1c")
print('inside ambas:', input_value)
root.destroy()
# --- main ---
root = tk.Tk() # PEP8: spaces around `=`
text_box = tk.Text(root, height=2, width=10)
text_box.pack()
button_commit = tk.Button(root, height=1, width=10, text="Aceptar", command=ambas)
button_commit.pack()
root.mainloop()
print('after mainloop:', input_value)
必须为全局变量赋值。使用
全局输入值
内部检索输入