huy666 发表于 2016-11-2 18:05:00

请教,python里怎样将一个类的内容显示到另一个类中。

比如
现在一个窗口显示三个按钮,两个文本。
一个按钮是退出。另两个按钮点击可以改变一个文本的内容。

我想让另一个文本(label_rtc)显示一个时钟。
网上搜到一个例子(类MyTimer)。

代码如下:
class MyTimer(QWidget):
    def __init__(self, parent = None):
      super(MyTimer, self).__init__(parent)      
      self.resize(200, 100)      
      self.setWindowTitle("QTimerDemo")
      
      self.lcd = QLCDNumber()      
      self.lcd.setDigitCount(10)      
      self.lcd.setMode(QLCDNumber.Dec)
      self.lcd.setSegmentStyle(QLCDNumber.Flat)
      self.lcd.display(time.strftime("%X",time.localtime()))

      layout = QVBoxLayout()
      layout.addWidget(self.lcd)      
      self.setLayout(layout)
      
      #新建一个QTimer对象      
      self.timer = QTimer()      
      self.timer.setInterval(1000)      
      self.timer.start()
         
      # 信号连接到槽      
      self.timer.timeout.connect(self.onTimerOut)

    # 定义槽
    def onTimerOut(self):      
      self.lcd.display(time.strftime("%X",time.localtime()))

class Pyqtp1(QtWidgets.QDialog, Ui_Dialog):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
      """
      Constructor
      
      @param parent reference to the parent widget
      @type QWidget
      """
      super(Pyqtp1, self).__init__(parent)
      self.setupUi(self)
   
    #@QtCore.pyqtSignature("")
    @QtCore.pyqtSlot()
    def on_btn1_clicked(self):
      """
      Slot documentation goes here.
      """
      # TODO: not implemented yet
      self.label.setText(u"按钮1被按下!")
      # raise NotImplementedError
   
    #@QtCore.pyqtSignature("")
    @QtCore.pyqtSlot()
    def on_btn2_clicked(self):
      """
      Slot documentation goes here.
      """
      # TODO: not implemented yet
      self.label.setText(u"按钮2被按下!")
      # raise NotImplementedError

if __name__ == "__main__":
    app = QApplication(sys.argv)
    dlg = Pyqtp1()
    dlg.show()
   
    t = MyTimer()
    t.show()      
   
    sys.exit(app.exec_())


现在是出来两个窗口,一个我之前的窗口,一个时钟窗口。
怎样做可以把时钟显示在之前窗口的文本label_rtc上?

Excellence 发表于 2016-11-2 20:11:07

给楼主提个建议,安装qt5.7,帮助有控件的属性和方法

huy666 发表于 2016-11-3 09:26:12

Excellence 发表于 2016-11-2 20:11
给楼主提个建议,安装qt5.7,帮助有控件的属性和方法

关键还是面向对象的编程方法。
以前用C,这一块有很大不同。

Excellence 发表于 2016-11-3 09:58:45

全局变量,继承等等都可以

huy666 发表于 2016-11-3 13:11:28

Excellence 发表于 2016-11-3 09:58
全局变量,继承等等都可以

是的,只是还不知道怎么处理。
页: [1]
查看完整版本: 请教,python里怎样将一个类的内容显示到另一个类中。