类中的Python公共方法?

时间:2016-07-10 12:33:33

标签: python class

我删除了代码以便更好地理解我的问题。问题是,如果有一些方法可以创建像公共方法这样的东西,以及我是如何或是否完全误解了OOP概念。

我有一个只播放视频的专业课PlayerWindow。然后有ControlsWindow课程仅用于我的开发,测试和维护目的(全屏关闭时启动)。因此,我想成为特别的人。无法弄清楚,如何从play()类调用方法ControlsWindow以及从内部调用,因为当我使用ControlsWindow实例初始化PlayerWindow时,我会得到无限循环。

class ControlsWindow(QtGui.QWidget):

  def __init__(self):
    super(ControlsWindow, self).__init__()

    self.playPauseButton = QtGui.QPushButton('Play', self)
    self.show()


class PlayerWindow(QtGui.QWidget):

  def __init__(self):
    super(PlayerWindow, self).__init__()

    # ...
    self.mediaPlayer = self.playerInstance.media_player_new() 
    # ... 
    self.initUI()
    self.play()

  def initUI(self):
    # ...
    self.show()
    self.controls_window = ControlsWindow()         

  def keyPressEvent(self, e):
    if e.key() == QtCore.Qt.Key_Return:
      self.toggleControlsWindow()

  def toggleControlsWindow(self):
    if self.isFullScreen():
      self.showNormal()
      self.controls_window = ControlsWindow()      
    else:
      self.controls_window.close()           
      self.showFullScreen()

  def play(self):
    self.mediaPlayer.play()


def main():
  app = QtGui.QApplication(sys.argv)
  player_window = PlayerWindow()

  sys.exit(app.exec_())               


if __name__ == "__main__":
  main()

2 个答案:

答案 0 :(得分:1)

您可以将PlayerWindow的实例传递给ControlsWindow的类构造函数:

class ControlsWindow(QtGui.QWidget):

  def __init__(self, parent):  # Notice the new parent argument
    super(ControlsWindow, self).__init__()
    self.parent = parent

    self.playPauseButton = QtGui.QPushButton('Play', self)
    self.show()

    # Now you can call the parent's (PlayerWindow) play() function:
    self.parent.play()


class PlayerWindow(QtGui.QWidget):

  def __init__(self):
    super(PlayerWindow, self).__init__()

    # ...
    self.mediaPlayer = self.playerInstance.media_player_new() 
    # ... 
    self.initUI()
    self.play()

  def initUI(self):
    # ...
    self.show()
    self.controls_window = ControlsWindow(self)  # Pass a reference of PlayerWindow

  def keyPressEvent(self, e):
    if e.key() == QtCore.Qt.Key_Return:
      self.toggleControlsWindow()

  def toggleControlsWindow(self):
    if self.isFullScreen():
      self.showNormal()
      self.controls_window = ControlsWindow(self)  # Pass a reference of PlayerWindow
    else:
      self.controls_window.close()           
      self.showFullScreen()

  def play(self):
    self.mediaPlayer.play()


def main():
  app = QtGui.QApplication(sys.argv)
  player_window = PlayerWindow()

  sys.exit(app.exec_())               


if __name__ == "__main__":
    main()

希望这有帮助!

答案 1 :(得分:1)

假设我理解正确,您的代码可以简化为:

# QtGui.QWidget
class B:
    pass

# ControlsWindow
class Test(B):
    def __init__(self):
        # You want to call Actual.play here?
        pass

# PlayerWindow
class Actual(B):
    def __init__(self):
        # Your self.mediaPlayer = self.playerInstance.media_player_new()
        self.some_variable = 42

    def play(self):
        print(self.some_variable)

如果您想从Actual.play课程内拨打Test方法,可以:

使Actual.play为静态,self.mediaPlayer为类变量

class Test(B):
    def __init__(self):
        # Here you go!
        Actual.play()

class Actual(B):
    # mediaPlayer = self.playerInstance.media_player_new()
    some_variable = 42

    def __init__(self):
        pass

    @staticmethod
    def play():
        print(Actual.some_variable)

或将对PlayerWindow对象的引用传递给您的ControlsWindow类实例

class B:
    pass

class Test(B):
    def __init__(self, actual: Actual):
        # Here you go!
        actual.play()

class Actual(B):
    def __init__(self):
        self.some_variable = 42

    def play(self):
        print(self.some_variable)