1. 程式人生 > >公開課 之 tony 電子時鐘 (課堂筆記)

公開課 之 tony 電子時鐘 (課堂筆記)

des %x 行程 設置背景圖片 自動填充 star init qpi RF

#   tony 之電子時鐘
from PyQt5.QtWidgets import QApplication, QWidget, QLCDNumber, QDesktopWidget, QVBoxLayout
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import time, sys

‘‘‘QLCDNumber 顯示數字 display()
.QDesktopWidget 測量桌面尺寸
QVBoxLayout 承載的盒子
‘‘‘
# pip install PyQt5
# 信號和槽函數

class MyTime( QWidget ):
‘‘‘
1:類, 2:數據, 3:方法
‘‘‘
def __init__(self): # 初始化,
super().__init__() #
self.initUI()
self.init_timer()



def up_time(self): # 更新時間
self.lcd.display( time.strftime(‘%X‘,time.localtime()) )

def init_timer(self):
self .timer = QTimer() # 定時器
self.timer.setInterval( 1000 ) # 設置每1秒觸發 timeout 信號
self.timer.start() # 啟動定時器
self.timer.timeout.connect( self.up_time )

def initUI(self): # 調整窗口組件大小,寬250px,高150px,
self.resize( 350,220 )
self.setWindowTitle( ‘斌彬電腦‘ ) # 標題
self.yi_dong()

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()) ) # 時間元祖 本地時間

self.box1 = QVBoxLayout() # 構建盒子總局
self.box1.addWidget( self.lcd ) # 要顯示的放進雲
self.box1.setAlignment( Qt.AlignCenter ) # 劇中
self.setLayout( self.box1 ) # 頂層頂層總局

palette1 = QPalette()
# palette1.setColor(self.backgroundRole(), QColor(192,253,123)) # 設置背景顏色
palette1.setBrush(self.backgroundRole(), QBrush(QPixmap(‘1.png‘))) # 設置背景圖片
self.setPalette(palette1)

# self.yan_se.setColor( QPalette.Background.Qt.darKYellow )
# self.setAutoFillBackground( True ) # 自動填充背景色
# self.setPalette( self.yan_se )

def yi_dong(self):
m_rect = self.frameGeometry() # 設置矩
w = QDesktopWidget().availableGeometry().center() #enter() # 獲取屏幕中間
m_rect.moveCenter( w )
self.move( m_rect.topLeft () ) # 從左上角開始移動直到中間


self.show() # 顯示界面


if __name__ == ‘__main__‘:
app = QApplication( sys.argv ) # 啟動
m_time = MyTime() # 運行程序
sys.exit( app.exec_() ) # 徹底退出

公開課 之 tony 電子時鐘 (課堂筆記)