简体   繁体   English

python tkinter在函数之间传递变量

[英]python tkinter passing variables between functions

I am trying to pass the variable dirpath into the export_data() function. 我正在尝试将变量dirpath传递给export_data()函数。 Export data runs off of a double click on a button located on a widget. 双击窗口小部件上的按钮即可导出数据。 Why is dirpath printing as: dirpath为什么打印为:

`<Tkinter.Event instance at 0x8ade56c>` 

instead of the actual path? 而不是实际路径?

def export_data(dirpath):
    print 'exporting...'
    print str(dirpath)
    os.mkdir('/home/bigl/Desktop/Library')
    shutil.copytree(dirpath, output_path)

When I run my code I get the error 运行代码时出现错误

exporting...
<Tkinter.Event instance at 0x8ade56c>
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
    return self.func(*args)
  File "/media/LOFTUS/20130308_searchtest.py", line 44, in export_data
    shutil.copytree(dirpath, output_path)
  File "/usr/lib/python2.7/shutil.py", line 169, in copytree
    names = os.listdir(src)
TypeError: coercing to Unicode: need string or buffer, instance found

In the body of the question you asked: 在问题正文中,您提出了以下问题:

Export data runs off of a double click on a button located on a widget. 双击窗口小部件上的按钮即可导出数据。 Why is dirpath printing as: dirpath为什么打印为:

<Tkinter.Event instance at 0x8ade56c>

When you bind to an event, the binding always sends an event object as a parameter to the bound function. 绑定到事件时,绑定始终将事件对象作为参数发送到绑定函数。 So, if you're doing: 因此,如果您正在做:

widget.bind("<Double-1>", export_data)

... then export_data will receive the event as it's only parameter. ...然后export_data将接收事件作为唯一参数。

To pass a variable, you need to use lambda or functools.partial or some sort of function generator. 要传递变量,您需要使用lambdafunctools.partial或某种函数生成器。 For example: 例如:

widget.bind("<Double-1>", lambda event: export_data(dirpath))

Be careful with this, however. 但是要小心。 The value passed to export_data will be the value of dirpath at the time the event occurs , which may be different than the value when you creating the binding. 传递给export_data的值将是事件发生时dirpath的值,该值可能与创建绑定时的值不同。

If you have a local variable that you want to pass to the function you can set it as a default value to a keyword argument, in which case the value at the time the lambda is created will be passed. 如果您有要传递给函数的局部变量,则可以将其设置为关键字参数的默认值,在这种情况下,将传递创建lambda时的值。

Example: 例:

path = some_function()
widget.bind("<Double-1>", lamba event, dirpath=path: export_data(dirpath))

Obviously Tkinter passes an event, not a string, to your callback. 显然, Tkinter将事件而不是字符串传递给您的回调。 If dirpath is a global variable (as you wrote before-- important information!), perhaps you meant to define your callback like this: 如果dirpath是一个全局变量(如您之前所写的重要信息!),那么您可能打算像这样定义回调:

def export_data(_ignored):
    print 'exporting...'
    print str(dirpath)
    os.mkdir('/home/bigl/Desktop/Library')
    shutil.copytree(dirpath, output_path)

Now your function can use the global dirpath (and output_path ). 现在,您的函数可以使用全局dirpath (和output_path )。 The way you had it, the argument declaration was hiding the global of the same name. 用它的方式,参数声明隐藏了同名的全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM