使用python Interpreter有点令人困惑

时间:2017-08-26 13:05:05

标签: python python-2.7 python-3.x kivy kivy-language

首先看到我制作的文件。有些事情让我感到困惑,这不是为了获得解决方案。这是关于python是如何工作的。

这是可以正常工作的代码: -

from kivy.support import install_twisted_reactor
install_twisted_reactor()
import dbm,pickle
from twisted.internet import reactor, protocol
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.screenmanager import ScreenManager,Screen,ObjectProperty
from kivy.uix.carousel import Carousel
from kivy.core.window import Window
from kivy.uix.tabbedpanel import TabbedPanel

Builder.load_string('''
<Blackmailing>:
    Button:
        text: 'Send'
        on_press: app.msg()
        size_hint: (0.3, 1)
''')

class ChatClient(protocol.Protocol):
    def connectionMade(self):
        self.factory.app.on_connect(self.transport)

    def dataReceived(self, data):
        self.factory.app.on_message(data)


class ChatClientFactory(protocol.ClientFactory):
    protocol = ChatClient

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

class Blackmailing(Screen):

    def send_black(self):
        return PracticeApp().msg("hello")
class PracticeApp(App):
    def build(self):
        print('-- connecting to')
        reactor.connectTCP("localhost", 8000,
                           ChatClientFactory(self))
        return Blackmailing()

    def msg(self):
        self.conn.write("hello")

    def on_connect(self, conn):
        print('-- connected')
        self.conn = conn

if __name__ == "__main__":
    PracticeApp().run()

在上面的程序中,您可以在单击按钮上看到一个函数被称为msg()。其中self.conn属性是同一个类的另一个函数。这个代码没有问题

这是类似的(确切的代码,几乎没有变化)但它不起作用: -

from kivy.support import install_twisted_reactor
install_twisted_reactor()
import dbm,pickle
from twisted.internet import reactor, protocol
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.screenmanager import ScreenManager,Screen,ObjectProperty
from kivy.uix.carousel import Carousel
from kivy.core.window import Window
from kivy.uix.tabbedpanel import TabbedPanel

Builder.load_string('''
<Blackmailing>:
    Button:
        text: 'Send'
        on_press: root.send_black()
        size_hint: (0.3, 1)
''')

class ChatClient(protocol.Protocol):
    def connectionMade(self):
        self.factory.app.on_connect(self.transport)

    def dataReceived(self, data):
        self.factory.app.on_message(data)


class ChatClientFactory(protocol.ClientFactory):
    protocol = ChatClient

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

class Blackmailing(Screen):

    def send_black(self):
        return PracticeApp().msg("hello")
class PracticeApp(App):
    def build(self):
        print('-- connecting to')
        reactor.connectTCP("localhost", 8000,
                           ChatClientFactory(self))
        return Blackmailing()

    def msg(self,message):
        self.conn.write(message)

    def on_connect(self, conn):
        print('-- connected')
        self.conn = conn

if __name__ == "__main__":
    PracticeApp().run()

在这里,你可以看到点击按钮我调用了一个root类,它是send_black(),在该类的函数send_black()中,我正在调用我直接调用的PracticeApp()类的msg()在msg()函数中,同一个类的另一个函数有self.conn属性,但这次出现的错误是“没有名为self.conn的属性”。为什么这样?

很抱歉,如果我无法让您了解问题。但我想有正确的理由。我将非常感谢能够告诉我正确理由的人。三江源

0 个答案:

没有答案