라즈베리파이 PyQt4를 이용한 알람시계 코드 질문입니다.
조회수 696회
gui를 이용한 알람시계를 구현하고 싶은데 알람을 설정하면 아래와 같은 에러들이 생성됩니다. 아래의 에러는 알람시간을 오전 00시부터 09시로 설정했을 때 생기는 에러입니다.
self.alarm_h = int(alarm_time[19:21]) #value of hour is sotred in index value 19 and 20
ValueError: invalid literal for int() with base 10: '0,'
아래는 00분부터 09분까지 알람을 설정했을 때 생기는 에러입니다.
self.alarm_m = int (alarm_time[23:25]) #value of minute is sotred in index value 23 and 24
ValueError: invalid literal for int() with base 10: '0,'
시간 값을 인덱스를 19에서 21로 설정하고 분 값을 23에서 25로 설정하는 것인데 이 인덱스를 코드 어디부분에서 받아오는 것인지 모르겠습니다.
import sys
import time
from PyQt4 import QtCore, QtGui #PyQt4 is used for designing the GUI
from time import strftime # To get time from Raspberry pi
#Code from Qt Designer
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.alarm_h = 0
self.alarm_m = 0
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(676, 439)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.label = QtGui.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setPointSize(14)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
self.gridLayout.addWidget(self.label, 3, 0, 1, 1)
self.Time_LCD = QtGui.QLCDNumber(self.centralwidget)
self.Time_LCD.setObjectName(_fromUtf8("Time_LCD"))
self.Time_LCD.setDigitCount(8)
self.Time_LCD.display(strftime("%H"+":"+"%M"+":"+"%S")) #get time from Pi and display it
self.gridLayout.addWidget(self.Time_LCD, 1, 0, 1, 3)
self.timer = QtCore.QTimer(MainWindow)
self.timer.timeout.connect(self.Time)
self.timer.start(1000)
current_time = QtCore.QTime()
self.Set_Time = QtGui.QTimeEdit(self.centralwidget)
self.Set_Time.setObjectName(_fromUtf8("Set_Time"))
self.Set_Time.setTime(current_time.currentTime())
self.gridLayout.addWidget(self.Set_Time, 2, 0, 1, 1)
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.button_pressed)
self.gridLayout.addWidget(self.pushButton, 2, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 676, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#End of code from Qt Designer
def retranslateUi(self, MainWindow): #update the GUI window
print("Dispay Re-translated")
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.label.setText(_translate("MainWindow", "Alarm curretly Turned off", None))
self.pushButton.setText(_translate("MainWindow", "Set Alarm", None))
def Time(self): #Function to compare current time with set time
self.Time_LCD.display(strftime("%H"+":"+"%M"+":"+"%S"))
self.current_h = int (strftime("%H"))
self.current_m = int (strftime("%M"))
if (self.current_h == self.alarm_h) and (self.current_m == self.alarm_m) and ((int(strftime("%S"))%15) == 0): #if the both time match
print("ALARM ON!!!!!")
message1 = " The time is " + str(self.alarm_h) + " : " + str(self.alarm_m) + " on " + strftime("%A")
message = "Sir, Good morning.. This is your wake up Alarm." + message1
self.label.setText(_translate("MainWindow",message1, None)) #display the message on GUI screen
time.sleep(1)
def button_pressed(self): #when set alarm button is pressed
print("Button Pressed")
alarm_time = str(self.Set_Time.time())
self.alarm_h = int(alarm_time[19:21]) #value of hour is sotred in index value 19 and 20
self.alarm_m = int (alarm_time[23:25]) #value of minute is sotred in index value 23 and 24
message = "Alarm is set at " + str(self.alarm_h) + " hours " + str(self.alarm_m) + " minutes"
self.label.setText(_translate("MainWindow", message, None)) #display the message on GUI screen
espeak.synth (message) #speak the message through audio jack
if __name__ == "__main__": #main function
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
1 답변
-
alarm_time = str(self.Set_Time.time()) self.alarm_h = int(alarm_time[19:21]) #value of hour is sotred in index value 19 and 20 self.alarm_m = int (alarm_time[23:25]) #value of minute is sotred in index value 23 and 24
Set_Time.time()
을 이용해서 시간을 표현하는 문자열을 가져온 다음 잘라서 사용하는 것 같은데0,
이라는 값이 넘어가면서 오류가 발생하는 것 같습니다.문자열로 가져온 후 다시 변환하는 것보다는 직접 시와 분을 가져오는 것이 좋을 것 같아요.
self.alarm_h = self.Set_Time.time().hour() self.alarm_m = self.Set_Time.time().minute()
로 바꾸시면 될 것 같습니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력