Skip to main content
 首页 » 编程设计

python之Kivy 和 XML RPC

2024年11月01日9birdshome

我一直在尝试在 kivy GUI 模块(我的服务器)和另一个 python 模块(我的客户端)之间进行通信。但到目前为止,我在运行 xml rpc 服务器以及 GUI run() 函数时遇到了问题。即使在线程中运行我的服务器后,我仍然有这个问题。 我希望有人对如何修复我的代码有建议,或者只是如何将 xml-rpc 与 kivy 一起使用。

这是我的代码:

import kivy 
 
kivy.require('1.7.1') 
 
from kivy.lang import Builder 
 
from kivy.uix.gridlayout import GridLayout 
 
from kivy.app import App 
 
from threading import Thread 
 
from kivy.clock import Clock 
 
Builder.load_file('kivy_gui.kv') 
 
class RoamClientInterface(GridLayout): 
 
    """ 
    Sets up connection with XMLRPC server 
 
    """ 
 
    move = False 
 
    """ 
    driveForward() -> Moves robot forward 
    """ 
 
    def driveForward(self): 
        self.move = True 
 
    """ 
    stop() -> stops robot from moving 
    """ 
 
    def stop(self): 
        self.move = False 
 
    def returnBool(self): 
        return self.move 
 
class ClientInterface(App): 
 
    def build(self): 
        return RoamClientInterface() 
 
    def sendCommands(dt): 
 
        print "start" 
        print "" 
        from SimpleXMLRPCServer import SimpleXMLRPCServer 
        server = SimpleXMLRPCServer(("localhost", 5000)) 
        print "initialize server" 
        print "" 
        server.register_instance(RoamClientInterface()) 
        print "register instance" 
        print "" 
        # while True: 
 
        try: 
            print "try handle request" 
            print "" 
            server.handle_request() 
            print "print handle request" 
            print "" 
        except KeyboardInterrupt: 
            import sys 
            sys.exit() 
 
if __name__ == '__main__': 
    serverThread = Thread(target=sendCommands(4)) 
    serverThread.start() 
   # Clock.schedule_once(sendCommands) 
    ClientInterface().run() 

请您参考如下方法:

我必须解决这个问题。 实际上有必要将它放入 RoamClientInterface 内部以使其工作,而不是像上面那样将它放入我的主要函数中。 如果有人需要帮助,我可以提供更多详细信息(显示代码)