I'm in the process of making an alarm clock GUI. My GUI has options to "Create Alarm", "Remove Alarm", and "View Alarms". In the Create Alarm option, I made a QDialog window consisting of 2 SpinBoxes (for a 24 hour clock) with a Cancel and OK button.
I'd like to have the user select the time they'd like, say 10:30, then to have that specific time saved into a variable and/or append it to an empty array after the user presses the OK button. So then I could hopefully carry that variable/array over into the "Remove Alarm" and "View Alarms" portions. Is this possible or should I go about it a different way?
Here is my code so far:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import datetime
# Start with empty array that will store appended alarms
alarms = []
class Window( QWidget ):
def __init__( self ):
super(Window, self).__init__()
self.initUI()
def initUI( self ):
self.setGeometry( 300, 300, 500, 600 )
self.setWindowTitle( "My Alarm Clock GUI" )
self.label = QLabel( self )
self.label.setText( "<h1>Alarm Clock</h1>" )
self.label.setAlignment( Qt.AlignCenter )
self.b1 = QPushButton( self )
self.b1.setText( "Create New Alarm" )
self.b1.clicked.connect( self.create_alarm_clicked )
self.b2 = QPushButton( self )
self.b2.setText( "Remove Alarm" )
self.b2.clicked.connect( self.remove_alarm_clicked )
self.b3 = QPushButton( self )
self.b3.setText( "View Alarms" )
self.b3.clicked.connect( self.view_alarm_clicked )
self.b4 = QPushButton( self )
self.b4.setText( "Quit" )
self.b4.clicked.connect( self.quit_clicked )
self.label_image = QLabel( self )
self.pixmap = QPixmap( "clock_pic_copy.png" )
self.label_image.setPixmap( self.pixmap )
self.label_image.setAlignment( Qt.AlignTop | Qt.AlignHCenter )
self.date_label = QLabel( self )
self.date_label.setAlignment( Qt.AlignCenter )
self.time_label = QLabel( self )
self.time_label.setAlignment( Qt.AlignCenter )
timer = QTimer( self )
timer.timeout.connect( self.DateAndTime )
timer.start( 1000 )
layout = QVBoxLayout( )
layout.setAlignment( Qt.AlignCenter )
layout.addWidget( self.label_image )
layout.addWidget( self.label )
layout.addWidget( self.date_label )
layout.addWidget( self.time_label )
layout.addWidget( self.b1 )
layout.addWidget( self.b2 )
layout.addWidget( self.b3 )
layout.addWidget( self.b4 )
self.setLayout(layout)
def DateAndTime(self):
current_date = QDate.currentDate()
current_time = QTime.currentTime()
# converting QDate & QTime object to string
label_time = current_time.toString( 'hh:mm:ss' )
label_date = current_date.toString('m-dd-yyyy')
# showing it to the label
self.date_label.setText(label_date)
self.time_label.setText( label_time )
# Create Alarm button function
def create_alarm_clicked( self ):
# Make QDialogButtonBox a subclass of QDialog
dialog = QDialog(self)
dialog.setGeometry( 425, 500, 200, 200 )
dialog.setWindowTitle( "Create Alarm" )
self.buttonBox = QDialogButtonBox(dialog)
self.buttonBox.setGeometry(QRect(10, 350, 161, 41))
self.buttonBox.setOrientation(Qt.Horizontal)
self.buttonBox.setStandardButtons( QDialogButtonBox.Cancel |
QDialogButtonBox.Ok )
self.buttonBox.setObjectName( "buttonBox" )
self.create_alarm_label = QLabel( self )
self.create_alarm_label.setText( "<h3>Please select time for the alarm:</h3>" )
self.create_alarm_text = QLabel(self)
self.create_alarm_text.setText( "Note: This app uses 24-hour time.")
# Spinbox for hours
sbox = QSpinBox( self )
sbox.valueChanged.connect( self.updateLabel )
sbox.setMaximum(23)
self.s_label = QLabel( '0', self )
# Spinbox for minutes
sbox2 = QSpinBox(self)
sbox2.valueChanged.connect( self.updateLabel)
sbox2.setMaximum(59)
self.s2_label = QLabel( '0', self )
# Inner layout which holds the Spinboxes
dialog.horizontalLayout = QHBoxLayout()
dialog.horizontalLayout.addWidget(sbox)
dialog.horizontalLayout.addWidget(sbox2)
# Outer layout for "Create Alarm" dialog
dialog.layout = QVBoxLayout()
dialog.layout.setAlignment( Qt.AlignCenter )
dialog.layout.addWidget( self.create_alarm_label )
dialog.layout.addWidget(self.create_alarm_text)
dialog.layout.addLayout(dialog.horizontalLayout)
dialog.layout.addWidget( self.buttonBox )
dialog.setLayout( dialog.layout )
dialog.exec_()
# Updates the Spin Box
def updateLabel( self, value ):
self.s_label.setText( str( value ) )
self.s2_label.setText( str( value ) )
# View Alarm function
def view_alarm_clicked( self, value ):
global alarms
self.view_messagebox = QMessageBox( self )
self.view_messagebox.setGeometry( 425, 500, 400, 400 )
self.view_messagebox.setWindowTitle( "View Alarms" )
self.view_messagebox.setStandardButtons( QMessageBox.Cancel | QMessageBox.Ok )
self.view_messagebox.setDefaultButton( QMessageBox.Ok )
if len(alarms) == 0:
self.view_messagebox.setText( "<h3>You have no alarms</h3>" )
self.view_messagebox.show()
self.view_messagebox.exec()
if len(alarms) > 0:
self.view_messagebox.setText( "<h3>You have alarms for:</h3> " + alarms)
self.view_messagebox.show()
self.view_messagebox.exec()
# Remove Alarm function
def remove_alarm_clicked( self ):
global alarms
self.remove_messagebox = QMessageBox( self )
self.remove_messagebox.setGeometry( 425, 500, 400, 400 )
self.remove_messagebox.setWindowTitle( "View Alarms" )
self.remove_messagebox.setStandardButtons( QMessageBox.Cancel | QMessageBox.Ok )
self.remove_messagebox.setDefaultButton( QMessageBox.Ok )
if len(alarms) == 0:
self.remove_messagebox.setText( "<h3>You have no alarms to delete</h3>" )
self.remove_messagebox.show()
self.remove_messagebox.exec()
elif len(alarms) > 0:
self.remove_messagebox.setText( "<h3>Please select alarm you'd like to delete</h3>" )
self.remove_messagebox.show()
self.remove_messagebox.exec()
# Quit Button functions
def quit_clicked( self ):
self.close()
def ring_alarm( self ):
pass
def run():
app = QApplication( [ ] )
GUI = Window()
GUI.show()
sys.exit( app.exec_() )
run()
The dialog of the Create Alarm portion of GUI:
question from:
https://stackoverflow.com/questions/65908590/how-to-save-data-from-qspinbox-into-variable-after-clicking-button 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…