扭曲的Tkinter

时间:2018-11-06 17:49:02

标签: python tkinter twisted

我一直在尝试Twisted和Tkinter。我有一个小程序正在工作,我可以单击Tkinter中的按钮,它将向本地服务器发送请求。这是通过调用MessageCFactory来完成的。这工作正常,但是当服务器返回其响应时,我的“已接收数据”方法无法捕获它。这可能是由于我不了解反应堆回路。

希望有人能指出我正确的方向

谢谢!

class Client(protocol.Protocol):


    def mainGUI(self):
    print("hello")
    now = datetime.datetime.now()
    root = Tk()
    tksupport.install(root)
    root.geometry("300x300+200+200")
    root.title('GUI')
    root.geometry('{}x{}'.format(460, 350))

    middleFrame = Frame(root)
    middleFrame.pack()

    Label(middleFrame, text="Initial IP").grid(row=0, column=2, sticky=W)
    initialIP = Entry(middleFrame, width=19)
    initialIP.grid(row=1, column=2, sticky=W)

    Label(middleFrame, text="Port").grid(row=2, column=2, sticky=W)
    initialPort = Entry(middleFrame, width=19)
    initialPort.grid(row=3, column=2, sticky=W)

    Label(middleFrame, text="Username").grid(row=4, column=2, sticky=W)
    username = Entry(middleFrame, width=19)
    username.grid(row=5, column=2, sticky=W)
    Button(middleFrame, text='Enter',
           command=lambda arg1=initialIP, arg2=username: self.select(initialIP, initialPort, username)).grid(
        row=6, column=5, sticky=W)
    print("GUI")


def select(self, initialIP, initialPort, username):
    initialIP = str(initialIP.get())
    initialPort = str(initialPort.get())

    """
    General data sanitation from the UI into things that our twisted application can understand
    Encoding as bytes for transfer
    """
    username = str(username.get())
    usernameBytes = str.encode(username)
    initialPort = int(initialPort)

    reactor.connectTCP(initialIP, initialPort, MessageCFactory(usernameBytes))
    print("message: %s" % (username))


class MessageCProto(protocol.Protocol):
def connectionMade(self):
    self.transport.write(self.factory.message)
    self.transport.loseConnection()


def dataReceived(self, data):
    print('echo:', data)

class MessageCFactory(ClientFactory):
protocol = MessageCProto


    def __init__(self, message):
    self.message = message

    def dataReceived(self, data):
    print('echo:', data)


def main():
reactor.callLater(0, Client)
reactor.run()

0 个答案:

没有答案