ตัวอย่างโปรแกรม นับเวลาถอยหลัง ด้วยภาษา Python
ตัวอย่างโปรแกรม นับเวลาถอยหลัง ด้วยภาษา Python 
- กำหนดเวลาได้ เป็น นาที (mm) และ วินาที (ss) 
- มีเสียงแจ้งเตือนก่อนหมดเวลา 30 วินาที และ 1 วินาที




from PyQt5 import QtWidgets
from PyQt5 import QtCore
import sys

from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, 
   QAction, QLineEdit, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
import winsound         # for sound  
import time   

DURATION_INT = 600

def secs_to_minsec(secs: int):
    mins = secs // 60
    secs = secs % 60
    minsec = f'{mins:02}:{secs:02}'
    return minsec

class App(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.time_left_int = DURATION_INT
        self.myTimer = QtCore.QTimer(self)
        # App window
        self.app = QApplication(sys.argv)
        self.win = QMainWindow()
        self.win.setGeometry(200, 200, 800, 500)
        self.win.setWindowTitle("Timer")
        # Widgets
        self.titleLabel = QtWidgets.QLabel(self.win)
        self.titleLabel.setText("Clock")
        self.titleLabel.move(50,20)
        self.timerLabel = QtWidgets.QLabel(self.win)
        self.timerLabel.setText("01:00")
        self.timerLabel.move(50,50)
        self.timerLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.timerLabel.setStyleSheet("font: 10pt Helvetica")
        self.startButton = QtWidgets.QPushButton(self.win)
        self.startButton.setText("Start")
        self.startButton.move(50,100)
        self.startButton.clicked.connect(self.startTimer)
        self.stopButton = QtWidgets.QPushButton(self.win)
        self.stopButton.setText("Stop")
        self.stopButton.move(50,130)
        self.mmLabel = QtWidgets.QLabel(self.win)
        self.mmLabel.setText(" mm  :   ss")
        self.mmLabel.move(180,20)
        self.mm = QtWidgets.QLineEdit(self.win)
        self.mm.move(180, 50)
        self.mm.setText("1")
        self.mm.resize(30,30)
        self.ss = QtWidgets.QLineEdit(self.win)
        self.ss.move(220, 50)
        self.ss.setText("00")
        self.ss.resize(30,30)
        self.setbutton = QtWidgets.QPushButton('Set', self.win)
        self.setbutton.move(180,100)
        self.setbutton.clicked.connect(self.setTime)   
        self.update_gui()
        # Show window
        self.win.show()
        sys.exit(app.exec_())

    def setTime(self):
        global DURATION_INT
        mmValue = '0'+self.mm.text()
        ssValue = '0'+self.ss.text()
        DURATION_INT = int(mmValue) * 60 + int(ssValue)
        self.time_left_int = DURATION_INT
        self.update_gui()

    def startTimer(self):
        self.time_left_int = DURATION_INT
        self.myTimer.timeout.connect(self.timerTimeout)
        self.myTimer.start(1000)
    def timerTimeout(self):
        self.time_left_int -= 1
        if self.time_left_int == 0:
            self.time_left_int = DURATION_INT
        self.update_gui()
    def update_gui(self):
        minsec = secs_to_minsec(self.time_left_int)
        self.timerLabel.setText(minsec)
        if (self.time_left_int in (30,1)) :
            winsound.Beep(440, 1250) # frequency, duration
            # time.sleep(0.25)        # in seconds (0.25 is 250ms)
            # winsound.Beep(600, 250)
            # time.sleep(0.25) 

app = QtWidgets.QApplication(sys.argv)
main_window = App()
main_window.show()
sys.exit(app.exec_())

สนใจเรียนพิเศษภาษา Python สามารถอ่านต่อ ได้ที่ลิงค์นี้ครับ https://pornchaiscratch.blogspot.com/



Create Date : 26 พฤษภาคม 2564
Last Update : 26 พฤษภาคม 2564 16:34:38 น.
Counter : 2047 Pageviews.

2 comments
  
pleasant post, stay aware of this fascinating work. It truly regards realize that this subject is being secured likewise on this site so cheers for setting aside time to talk about this! [url=https://www.thwwindows.com/product/aluminum-bifold-doors]go now[/url]
โดย: article online (สมาชิกหมายเลข 7343730 ) วันที่: 22 ธันวาคม 2565 เวลา:20:15:30 น.
  
pleasant post, stay aware of this fascinating work. It truly regards realize that this subject is being secured likewise on this site so cheers for setting aside time to talk about this! go now
โดย: john (สมาชิกหมายเลข 7353478 ) วันที่: 22 ธันวาคม 2565 เวลา:20:16:49 น.
ชื่อ : * blog นี้ comment ได้เฉพาะสมาชิก
Comment :
 *ส่วน comment ไม่สามารถใช้ javascript และ style sheet
 

thaiger_u
Location :
  

[ดู Profile ทั้งหมด]
 ฝากข้อความหลังไมค์
 Rss Feed
 Smember
 ผู้ติดตามบล็อก : 3 คน [?]



Dancinga
@ฟรี
โปรแกรมปฏิทิน 2564 - Free android app


@ รับสอน เขียนโปรแกรม Python ระดับมัธยมปลาย
พฤษภาคม 2564

 
 
 
 
 
 
1
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
27
29
30
31
 
 
All Blog