Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter:按下按钮时读取并存储文本_Python_Function_User Interface_Tkinter_Button - Fatal编程技术网

Python Tkinter:按下按钮时读取并存储文本

Python Tkinter:按下按钮时读取并存储文本,python,function,user-interface,tkinter,button,Python,Function,User Interface,Tkinter,Button,这段代码-应该-读取文本小部件中的文本,并在按下按钮时执行一个函数,该函数接受该文本并将其作为某个变量的值返回。但有两个或更多的问题——正如我所看到的那样 第一:我犯了这个错误 from tkinter import * from functools import * textin = Tk() fr1 = Frame(textin,width='90',height='8') e = Text(fr1,bg='white',font=('Calibre',16),width='84',heig

这段代码-应该-读取文本小部件中的文本,并在按下按钮时执行一个函数,该函数接受该文本并将其作为某个变量的值返回。但有两个或更多的问题——正如我所看到的那样

第一:我犯了这个错误

from tkinter import *
from functools import *
textin = Tk()
fr1 = Frame(textin,width='90',height='8')
e = Text(fr1,bg='white',font=('Calibre',16),width='84',height='8')
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def click1(t8):
    t7 = t8.split()
    if len(t7) > 0 :
        b1.configure(text='Recalculate')
        t = " " + t8.strip().lower()+" "
    return t
de1 = partial(click1)
print(click1(e.get(1.0,END)))
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
b1 = Button(textin,text="Calculate",bg='#E3E6FF',font=("Calibre Bold",16),borderwidth='1'
,relief="flat",justify=CENTER,width=16,fg="#010346",activebackground='#B6D1FF',
activeforeground='#010346',highlightbackground='#DCE8FF',state=NORMAL,command=de1)
第二:当我试图解决问题时,函数
ckick1
不会给出任何结果,而是会更改按钮文本,我无法找到使此按钮存储文本的方法

我应该如何解决这些问题

UnboundLocalError: local variable 't' referenced before assignment
提前感谢:)

注:新学员



函数似乎可以返回
t
,而无需执行
if
语句,因此,要么在函数开头将
t
定义为
t=None
或类似,要么首先将
return
放在
if语句下
partial(单击1)
与只需单击1
相同。其次,如果
len(t7)
0
,那么变量
t
将是未定义的。还有,为什么您要从按钮回调返回一些内容?它将被刮取,结果不会显示在任何地方。@Matiss我试图将它放在if语句下,但该函数只在第一次单击时工作。该函数在第一次单击时不工作,它在按钮创建之前就已经运行了<代码>打印(单击1(e.get(1.0,END))立即运行,而不是单击按钮。@TheLizzard I将修复第二点。我尝试了
partial
,因为函数
click1
有一个变量,我不知道如何让按钮执行带有一个或多个参数的函数。
     ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++