如果我正在编写一个想要通过使用颜色来传达某些信息的应用程序,我该如何更改给定窗口小部件的背景和前景色?如果可能的话,我想知道如何在林间空地中做到这一点,以及以编程方式(对于计算的颜色).
我想知道如何对复杂的小部件执行此操作,例如,HBox包含一个VBox包含一些的小部件Labels.
理想情况下,这还包括一个解决方案,允许我对小部件的现有颜色进行着色,并识别主题使用的任何图像的平均颜色,以便我可以编程方式补偿任何可能使文本无法读取或以其他方式发生冲突的颜色选择 - 但如果我能把按钮变成红色,我会很高兴的.
我想知道为什么以下不起作用.
from twisted internet import defer, reactor
from twisted.python.failure import Failure
import twisted.names.client
def do_lookup(do_lookup):
d = twisted.names.client.getHostByName(domain)
d.addBoth(lookup_done)
def lookup_done(result):
print 'result:', result
reactor.stop()
domain = 'twistedmatrix.com'
reactor.callLater(0, do_lookup, domain)
reactor.run()
Run Code Online (Sandbox Code Playgroud)
结果是:
result: [Failure instance: Traceback
(failure with no frames): <class
'twisted.names.error.ResolverError'>:
Stuck at response without answers or
delegation ]
Run Code Online (Sandbox Code Playgroud) 我想问一个关于如何在扭曲中关闭连接的问题RPC.
我知道有一个类似的问题,但它似乎没有回答我的问题.
我正在做一些基本连接,如下图所示:
cfactory = pb.PBClientFactory()
reactor.connectTCP(<host>, <port>, cfactory)
dfr.addCallbacks(<callback>, <errfun>, ...)
...
(in the <callback> func) remote.callRemote('myfunc', ...)
Run Code Online (Sandbox Code Playgroud)
这一切都有效,并做我需要的东西.
但麻烦的是,如果我检查它,我看到连接仍然有效("ESTABLISHED")
netstat -a.
由于我在无限期运行的客户端和服务器之间执行此操作,因此我不能只是继续累积活动连接.
出于同样的原因,我无法阻止反应堆.
那么,有没有办法关闭连接,而不是创建一个自己的协议?
我想首先检查一下,因为除了这一个事实之外它全部处于正常工作状态 - 如果可能的话我只需要添加一个所需的东西,而不是从协议设置开始.
感谢您的关注,任何一般建议将不胜感激.
托尼
我有一个项目本质上是一个游戏服务器,用户通过telnet连接和发送文本命令.
代码是在C中,非常古老和非模块,并有几个错误和缺少功能.仅主要功能是代码的一半.
我得出的结论是,使用Twisted在Python中重写它实际上可以带来更快的完成,除了其他好处.
所以,问题是:我应该使用哪些软件包和模块?我在"protocols"包中看到了一个"telnet"模块.我还看到"cronch"包带有"ssh"和另一个"telnet"模块.
我是Python的完全新手.
如何运行试验以便它执行目录中的所有测试?如果我单独对每个文件运行试验,我的所有单元测试都会通过,但如果我尝试类似...
trial test/
Run Code Online (Sandbox Code Playgroud)
在测试目录上,它给了我一个" PASSED",以及以下消息......
UserWarning: (for module __init__) not in path importer cache (PEP 302 violation
- check your local configuration).
Run Code Online (Sandbox Code Playgroud)
而不是实际运行目录中的所有测试.
所以我有这个小方案项目,它使用来自高速公路扭曲的python中的websockets.客户端和服务器之间的连接以及数据传输工作完美,但在TCP连接关闭后我无法正常退出程序.她是代码:
class MyClientProtocol(WebSocketClientProtocol):
def onConnect(self, response):
print("Server connected: {0}".format(response.peer))
def onOpen(self):
print("WebSocket connection open.")
def hello():
from twisted.internet import reactor
self.sendMessage(u"Hello, world!".encode('utf8'))
self.sendMessage(b"\x00\x01\x03\x04", isBinary=True)
#self.factory.reactor.callLater(1, hello)
#self.reactor.callFromThread(reactor.stop)
#reactor.callFromThread(reactor.stop)
#self.factory.reactor.callFromThread(reactor.stop)
# start sending messages every second ..
hello()
return
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
def WebCon():
import sys
from twisted.python import log
from twisted.internet import reactor
log.startLogging(sys.stdout)
factory = WebSocketClientFactory("ws://localhost:8080", debug=False) …Run Code Online (Sandbox Code Playgroud)