首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyQt5 -封装/使组件可重用-示例: QTimer/QLabel在父类之外的实现

PyQt5 -封装/使组件可重用-示例: QTimer/QLabel在父类之外的实现
EN

Stack Overflow用户
提问于 2022-01-14 15:57:12
回答 1查看 83关注 0票数 0

我实现了一个标签,在我的几个PyQt5应用程序中显示当前时间。这是一份MRE:

代码语言:javascript
复制
import sys 
import logging

from PyQt5 import QtWidgets, QtCore, QtGui

__log__ = logging.getLogger()


class App(QtWidgets.QApplication):
    
    def __init__(self, sys_argv):
        super(App, self).__init__(sys_argv)
        self.main_view = MainView()
        self.main_view.show()


class MainView(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setObjectName("MreUi")
        self.resize(300, 100)
        self.setWindowTitle('MreUi')
        self.label = QtWidgets.QLabel()
        self.setCentralWidget(self.label)
        config_clock(self.label)
        self.start_clocks()
        
    def start_clocks(self):
        timer = QtCore.QTimer(self)
        timer.timeout.connect(self.show_time)  
        timer.start(1000)
        
    def show_time(self):
        current_time = QtCore.QTime.currentTime()
        clock_label_time = current_time.toString('hh:mm:ss')
        self.label.setText(clock_label_time)


def config_clock(label):
    label.setAlignment(QtCore.Qt.AlignCenter)
    font = QtGui.QFont('Arial', 24, QtGui.QFont.Bold)
    label.setFont(font)
        
        
if __name__ == '__main__':
    logging.basicConfig()
    app = App(sys.argv)
    try:
        sys.exit(app.exec_())
    except Exception as e:
        __log__.error('%s', e)

当我在我的几个PyQt应用程序中实现了类似的时钟时,我觉得最好将它实现为一个组件/封装它。首先,我想通过从任何config_clock调用一个QWidget函数来实现这一点,并让该函数为指定的标签实现时钟的所有工作。这将避免在编写/调用start_clocks和show_time MainView实例方法时在多个应用程序中重复自己。但当我开始编码时..。

代码语言:javascript
复制
# from inside my QWidget:
config_clock(self.label)

# this function would live outisde the class, thus reusable by diff Qt apps:
def config_clock(label):
   # start the clock
   # set default font, etc for label
   # instantiate QtCore.QTimer
   # # but that's when I realized I've always passed self to QtCore.QTimer and that maybe encapsulating this isn't as trivial as I thought.

我是否应该创建自己的ClockLabel()对象来传递QtWidget的标签,并且也可以是可能需要它的每个QtWidget的实例属性?我闻起来有点笨重。但是肯定有一种方法可以在PyQt中实现“可重用组件”,我只是不知道.

如果要将MainView(QtWidgets.QMainWindow)作为参数传递给我编写的ClockLabel()类或签名可能类似于的config_clock函数,我也不确定是否可以将它正确地称为“父类”:

代码语言:javascript
复制
def config_clock(label, parent_qtwidget):
# also feels clunky and not sure if parent would be the right term

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-14 16:16:16

使用QtWidgets,通过继承来专门化小部件是正常的。下面是一个如何重新安排代码以生成可重用小部件的示例:

代码语言:javascript
复制
class ClockLabel(QtWidgets.QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.setAlignment(QtCore.Qt.AlignCenter)
        font = QtGui.QFont('Arial', 24, QtGui.QFont.Bold)
        self.setFont(font)

        self._timer = QtCore.QTimer(self)
        self._timer.timeout.connect(self._show_time)

    def start(self):
        self._timer.start(1000)

    def stop(self):
        self._timer.stop()

    def _show_time(self):
        current_time = QtCore.QTime.currentTime()
        clock_label_time = current_time.toString('hh:mm:ss')
        self.setText(clock_label_time)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70713273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档