I am a beginner in programming with Python. I am going to start my first job in this specialization. Can I use this code for presenting my skills at an interview?
import os
import time
import datetime
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class MainForm(QtWidgets.QMainWindow):
# atribute to save variable from time-edit widget, which is passed to the second window.
saved_time = None
def __init__(self, **kwargs):
super().__init__(**kwargs)
# settings of size and title
self.setWindowTitle("TurnOff Aplikace")
self.setGeometry(500, 300, 404, 280)
self.setFixedSize(404, 280)
# create stacked widget
self.Qtstack = QtWidgets.QStackedWidget()
self.setCentralWidget(self.Qtstack)
# create two widget to stacked-widget
self.stack1 = QtWidgets.QWidget()
self.stack2 = QtWidgets.QWidget()
# add widget to stacked widget
self.Qtstack.addWidget(self.stack1)
self.Qtstack.addWidget(self.stack2)
# setting of layout to first stack
self.layout_stack1 = QtWidgets.QVBoxLayout()
self.stack1.setLayout(self.layout_stack1)
# setting of layout to second stack
self.layout_stack2 = QtWidgets.QVBoxLayout()
self.stack2.setLayout(self.layout_stack2)
# launch both windows
self.show()
self.front_window()
self.back_window()
def front_window(self):
# layout with text
self.text = QtWidgets.QWidget()
self.layout_text = QtWidgets.QHBoxLayout()
self.text.setLayout(self.layout_text)
self.layout_stack1.addWidget(self.text)
self.layout_stack1.addStretch()
# Layout to edit a checkboxs
layout_edit = QtWidgets.QHBoxLayout()
self.layout_stack1.addLayout(layout_edit)
self.layout_stack1.addStretch()
# Layout to error issue
layout_error = QtWidgets.QHBoxLayout()
self.layout_stack1.addLayout(layout_error)
# Layout to pushbutton
layout_button = QtWidgets.QHBoxLayout()
self.layout_stack1.addLayout(layout_button)
# widget with text
self.text = QtWidgets.QLabel("Here you set how long it takes you to turn off the PC\nautomatically. You can also specify which monitor you want\nto switch to immediately. The minimum time is 15 minutes. \n\nNote: \nBefore turning off, the PC will automatically switch to the\nfirst monitor.")
self.text.setFont(QtGui.QFont('Arial', 10))
self.layout_text.addWidget(self.text)
# Widget with time-edite
layout_edit.addStretch()
self.time_edit = QtWidgets.QTimeEdit(self)
self.time_edit.setFont(QtGui.QFont('Arial', 16))
layout_edit.addWidget(self.time_edit)
# save time from edit-time to class variables
self.saved_time = self.time_edit
# Widget s checkboxes
layout_edit.addStretch()
self.checkbox1 = QtWidgets.QCheckBox("1. monitor", self)
self.checkbox1.setFont(QtGui.QFont('Arial', 10))
layout_edit.addWidget(self.checkbox1)
layout_edit.addStretch()
self.checkbox2 = QtWidgets.QCheckBox("2. monitor", self)
self.checkbox2.setFont(QtGui.QFont('Arial', 10))
layout_edit.addWidget(self.checkbox2)
layout_edit.addStretch()
#widget s error issue
self.error = QtWidgets.QLabel(self)
self.error.setText("<font color='red'>minimum time is 15 minutes</font>")
self.error.setFont(QtGui.QFont('Arial', 14))
layout_error.addWidget(self.error)
self.error.hide()
# Widget with pushbutton
self.Push_button_FW = QtWidgets.QPushButton("Enter", self)
layout_button.addWidget(self.Push_button_FW)
# logic with pushbutton
self.Push_button_FW.clicked.connect(self.check_minimum_edit_time)
# logic with checkboxs
self.checkbox1.clicked.connect(self.checkbox_1_clicked)
self.checkbox2.clicked.connect(self.checkbox_2_clicked)
def back_window(self):
# layout with text and countdown timer
self.text_countdown = QtWidgets.QWidget()
layout_text_countdown = QtWidgets.QHBoxLayout()
self.text_countdown.setLayout(layout_text_countdown)
self.layout_stack2.addWidget(self.text_countdown)
# Layout with pushbutton
button_layout = QtWidgets.QHBoxLayout()
self.layout_stack2.addLayout(button_layout)
# widget with text
layout_text_countdown.addStretch()
self.text = QtWidgets.QLabel("Automatic turning off the PC !!")
self.text.setFont(QtGui.QFont('Arial', 10))
layout_text_countdown.addWidget(self.text)
layout_text_countdown.addStretch()
# widget with countdown timer
self.countdown = QtWidgets.QLabel(self)
self.countdown.setFont(QtGui.QFont('Arial', 16))
layout_text_countdown.addWidget(self.countdown)
layout_text_countdown.addStretch()
# Widget with pushbutton
self.Push_button_BW = QtWidgets.QPushButton("Stop", self)
button_layout.addWidget(self.Push_button_BW)
# logic with pushbutton
self.Push_button_BW.clicked.connect(self.change_windows)
self.Push_button_BW.clicked.connect(self.stop_countdown)
def check_minimum_edit_time(self):
"""
The function monitors whether the user has entered a minimum value in the countdown time.
"""
given_time = self.time_edit.time()
if given_time >= QtCore.QTime(0, 15):
self.change_windows()
self.start_countdown()
self.change_monitor()
else:
self.error.show()
def change_windows(self):
"""
The function changes windows in the application.
"""
if self.Qtstack.currentIndex() == 0:
self.Qtstack.setCurrentIndex(1)
elif self.Qtstack.currentIndex() == 1:
self.Qtstack.setCurrentIndex(0)
def change_monitor(self):
"""
The function changes the output from the PC to the monitor.
"""
if self.checkbox1.isChecked():
os.system("DisplaySwitch.exe / internal")
elif self.checkbox2.isChecked():
os.system("DisplaySwitch.exe / external")
def checkbox_1_clicked(self):
"""
The function ensures that only one checkbox is unchecked at a time.
"""
if self.checkbox1.isChecked():
self.checkbox1.setChecked(True)
self.checkbox2.setChecked(False)
else:
self.checkbox1.setChecked(False)
def checkbox_2_clicked(self):
"""
The function ensures that only one checkbox is unchecked at a time.
"""
if self.checkbox2.isChecked():
self.checkbox2.setChecked(True)
self.checkbox1.setChecked(False)
else:
self.checkbox2.setChecked(False)
def start_countdown(self):
"""
The function starts the countdown when the ENTER button is clicked.
"""
# create string from time
string_time = self.saved_time.time().toString()
# string pulls the time, but only in seconds, and stores a variable that is inserted into the function: timer_start ()
x = time.strptime(string_time.split(',')[0],'%H:%M:%S')
self.seconds = (datetime.timedelta(hours=x.tm_hour,minutes=x.tm_min,seconds=x.tm_sec).total_seconds())
self.timer_start()
def stop_countdown(self):
"""
The function cancels the time countdown.
"""
self.my_qtimer.stop()
def timer_start(self):
"""
The function sets the countdown.
"""
self.time_left_int = self.seconds
self.my_qtimer = QtCore.QTimer(self)
self.my_qtimer.timeout.connect(self.timer_timeout)
self.my_qtimer.start(1000)
def timer_timeout(self):
"""
The function sets the end of the countdown.
"""
if self.time_left_int > 0:
self.time_left_int -= 1
self.update_number()
else:
self.my_qtimer.stop()
os.system("shutdown / s / t 1")
def update_number(self):
"""
The function resets the number every second so that the user can see the countdown.
"""
self.countdown.setText(str(datetime.timedelta(seconds=self.time_left_int)))
class App(QtWidgets.QApplication):
def __init__(self):
super().__init__(sys.argv)
def build(self):
self.main_window = MainForm()
sys.exit(self.exec_())
root = App()
root.build()
-
\$\begingroup\$ This is interesting code because I didn't know you could use Python to shut down a computer. But for an interview, it might not be that interesting to watch someone shut down their computer. \$\endgroup\$bfris– bfris2020年09月11日 04:31:34 +00:00Commented Sep 11, 2020 at 4:31
-
\$\begingroup\$ My first interview for programming job was successful with this code :D \$\endgroup\$Tomas– Tomas2020年09月12日 10:33:09 +00:00Commented Sep 12, 2020 at 10:33
-
\$\begingroup\$ I stand corrected. Congratulations! \$\endgroup\$bfris– bfris2020年09月12日 13:54:04 +00:00Commented Sep 12, 2020 at 13:54
1 Answer 1
Don't mix locales
"Aplikace" seems to be Czech, where everything else is English. Choose one or the other consistently. If you want to switch between the two based on the system's locale that is also an option.
Stack switching
if self.Qtstack.currentIndex() == 0:
self.Qtstack.setCurrentIndex(1)
elif self.Qtstack.currentIndex() == 1:
self.Qtstack.setCurrentIndex(0)
can be
self.Qtstack.setCurrentIndex(
1 - self.Qtstack.currentIndex()
)
No-op check
Does this actually have any effect?
if self.checkbox1.isChecked():
self.checkbox1.setChecked(True)
else:
self.checkbox1.setChecked(False)
I think the only thing that needs to be preserved there is the modification of checkbox2
.
Time splitting
This:
time.strptime(string_time.split(',')[0],'%H:%M:%S')
is worrisome. Why is there a comma in that string? If it's actually because a comma precedes fractional seconds, then don't discard that section of the string using a split
; instead include ,%f
at the end of your format string.
Subprocess
Replace calls to os.system
with calls to subprocess
.
-
\$\begingroup\$ Thank you for your response. I would like to ask more about classes, because I dont know if what I wrote is good design due to functions which occur time in my app. Should I make a new class for them or is this ok ? \$\endgroup\$Tomas– Tomas2020年06月25日 17:54:08 +00:00Commented Jun 25, 2020 at 17:54
-
\$\begingroup\$ The class layout seems fairly reasonable. \$\endgroup\$Reinderien– Reinderien2020年06月25日 17:56:14 +00:00Commented Jun 25, 2020 at 17:56