[英]How to Access Tkinter Widgets Defined in Other Function Without Global Variables?
[英]How to use Global Variables defined in Function loops
对于我的简单套接字服务器 ,我需要有一个名为serverloop
的函数(我正在尝试提高我的函数技巧。)在此函数中,循环不断尝试与潜在的客户端连接:
def serverloop(s):
while True:
conn, addr = s.accept()
print "Connected with", addr
但是,需要conn
才能运行其他功能:
def send_msg(conn):
#Send some data to the remote server
my_message = raw_input(">>>")
#set the whole string
conn.sendall(my_message)
我试图用line global conn
强制conn成为global,但是我仍然收到一个错误:
NameError: global name 'conn' is not defined
注意:我必须使用线程。
尽管在函数中定义了conn变量,但我该如何调用呢?
您正在使用线程,并且在调用RECEIVE
时尚未设置 conn
。
解决方法是要么不使用线程 ,要么至少将conn
设置为None
然后在尝试将值传递给RECEIVE
之前轮询它是否设置为main
的其他值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.