To display system clock time in LCD format - python

I just want to display system clock time in LCD format. I also want the time to be displayed using hh:mm:ss format. My code is below. But when I run it, it is not as I expected. Can anyone explain why?
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
timer = QtCore.QTimer(self)
timer.timeout.connect(self.showlcd)
timer.start(1000)
self.showlcd()
def initUI(self):
self.lcd = QtGui.QLCDNumber(self)
self.setGeometry(30, 30, 800, 600)
self.setWindowTitle('Time')
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.lcd)
self.setLayout(vbox)
self.show()
def showlcd(self):
time = QtCore.QTime.currentTime()
text = time.toString('hh:mm:ss')
self.lcd.display(text)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

QLCDNumber has a fixed digit display (QLCDNumber.digitCount) and its default value is 5. So your text is truncated to the last 5 characters. You should set an appropriate value (8 in your case).
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
timer = QtCore.QTimer(self)
timer.timeout.connect(self.showlcd)
timer.start(1000)
self.showlcd()
def initUI(self):
self.lcd = QtGui.QLCDNumber(self)
self.lcd.setDigitCount(8) # change the number of digits displayed
self.setGeometry(30, 30, 800, 600)
self.setWindowTitle('Time')
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.lcd)
self.setLayout(vbox)
self.show()
def showlcd(self):
time = QtCore.QTime.currentTime()
text = time.toString('hh:mm:ss')
self.lcd.display(text)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

Related

QCalendarWidget Renders Small

I am trying to use the QCalendarWidget but it doesn't render in the user interface as expected. The examples that I have seen show a calendar picker like object, but in my case I get a quite small rendering of a field. Here's what it looks like in the UI:
This is my first time using it so I am not sure if I am missing a step. Any thoughts on what I could be doing incorrectly? Here is the complete code being used:
from PyQt5.QtWidgets import QMainWindow, QCalendarWidget, QLabel
from PyQt5 import QtCore, QtWidgets, QtGui
import sys
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
cal = QCalendarWidget(self)
cal.setGridVisible(True)
cal.move(20, 20)
cal.clicked[QtCore.QDate].connect(self.showDate)
self.lbl = QLabel(self)
date = cal.selectedDate()
self.lbl.setText(date.toString())
self.lbl.move(20, 200)
self.setGeometry(100,100,300,300)
self.setWindowTitle('Calendar')
self.show()
def showDate(self, date):
self.lbl.setText(date.toString())
def main():
app = QtWidgets.QApplication(sys.argv)
mainWin = Example()
mainWin.show()
sys.exit( app.exec_() )
if __name__ == '__main__':
main()
Use a layout, for example a QVBoxLayout, in the centralWidget of QMainWindow:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Example(QtWidgets.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
cal = QtWidgets.QCalendarWidget(gridVisible=True)
cal.clicked.connect(self.showDate)
self.lbl = QtWidgets.QLabel()
date = cal.selectedDate()
self.lbl.setText(date.toString())
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QVBoxLayout(central_widget)
lay.addWidget(cal)
lay.addWidget(self.lbl)
self.setGeometry(100, 100, 300, 300)
self.setWindowTitle("Calendar")
#QtCore.pyqtSlot(QtCore.QDate)
def showDate(self, date):
self.lbl.setText(date.toString())
def main():
app = QtWidgets.QApplication(sys.argv)
mainWin = Example()
mainWin.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

change label from other class in a different file

I am creating an application where I have a main window whit a label and then a docked widget that is in another file. I want to change the main windows label from a button at the docked widget. I try to import the main window file but then I can not access to the label. And I also tried to call a function in the main windows that changes the label but then the label does not change.
Here is the code:
main_window.py:
import results_window
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.define_main_windows()
self.create_dock_widgets()
def define_main_windows(self):
# Define de Main window properties
self.setMinimumSize(QSize(300, 100))
self.setWindowTitle("Python SkyLibris")
self.setWindowIcon(QtGui.QIcon("skylibris_icon.png"))
self.setStyleSheet("QMainWindow {background: 'white';}")
self.top = 50
self.left = 0
self.width = 1300
self.height = 400
self.setGeometry(self.left, self.top, self.width, self.height)
self.result = QLabel("result:")
self.setCentralWidget(self.result)
def create_dock_widgets(self):
# Create dock widgets
self.results_window = results_window.results_window()
self.resultsWindowDock = QDockWidget("Results Viewer", self)
self.resultsWindowDock.setWidget(self.results_window )
self.resultsWindowDock.setFloating(False)
self.resultsWindowDock.setVisible(True)
self.addDockWidget(Qt.LeftDockWidgetArea, self.resultsWindowDock)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
results_window.py:
import main_window
class results_window(QWidget):
def __init__(self):
super(results_window, self).__init__()
print("init")
self.label = QLabel()
self.value = QLineEdit()
self.bt = QPushButton("Click")
self.bt.clicked.connect(self.clickMethod)
self.main_layout = QVBoxLayout()
self.main_layout.addWidget(self.label)
self.main_layout.addWidget(self.value)
self.main_layout.addWidget(self.bt)
self.setLayout(self.main_layout)
def clickMethod(self):
print(self.value.text())
text = self.value.text()
main_window.result.setText(text)
You are using the wrong tools, for example your code has a circular import that causes your application to close since it is equivalent to a while True.
In Qt, signals and slots are used to share data asynchronously, as well as contributing to the fact that there is no coupling between classes. In your case, Results_Window must have a signal that transmits that information to the MainWindow, this signal must be emit within clickMethod.
results_window.py
from PyQt5 import QtCore, QtWidgets
class Results_Window(QtWidgets.QWidget):
resultChanged = QtCore.pyqtSignal(str)
def __init__(self):
super(Results_Window, self).__init__()
print("init")
self.label = QtWidgets.QLabel()
self.value = QtWidgets.QLineEdit()
self.bt = QtWidgets.QPushButton("Click")
self.bt.clicked.connect(self.clickMethod)
main_layout = QtWidgets.QVBoxLayout(self)
main_layout.addWidget(self.label)
main_layout.addWidget(self.value)
main_layout.addWidget(self.bt)
#QtCore.pyqtSlot()
def clickMethod(self):
text = self.value.text()
self.resultChanged.emit(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
w = Results_Window()
w.show()
sys.exit(app.exec_())
main_window.py
from PyQt5 import QtCore, QtGui, QtWidgets
import results_window
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.define_main_windows()
self.create_dock_widgets()
def define_main_windows(self):
self.setMinimumSize(QtCore.QSize(300, 100))
self.setWindowTitle("Python SkyLibris")
self.setWindowIcon(QtGui.QIcon("skylibris_icon.png"))
self.setStyleSheet("QMainWindow {background: 'white';}")
top, left, width, height = 50, 0, 1300, 400
self.setGeometry(left, top, width, height)
self.result = QtWidgets.QLabel("result:")
self.setCentralWidget(self.result)
def create_dock_widgets(self):
self.results_window = results_window.Results_Window()
self.results_window.resultChanged.connect(self.result.setText)
self.resultsWindowDock = QtWidgets.QDockWidget("Results Viewer", self)
self.resultsWindowDock.setWidget(self.results_window )
self.resultsWindowDock.setFloating(False)
self.resultsWindowDock.setVisible(True)
self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.resultsWindowDock)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
I had similar problem in PyQT5 where I was unable to access and set the local variables. After a lot of struggle I found writing to file and reading from file as the best solution. Simply write the desired output to file and access the same info from other file. Works great!

How to make a button move as the window size is adjusted? (PyQt4)

I know the 2nd line I commented out doesn't work, just a representation of what I was thinking. This would be running the whole time the program is, so it could adjust to size changes.
Is something like this possible?
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 500, 300)
self.setWindowTitle("SciCalc")
self.setWindowIcon(QtGui.QIcon('atom.png'))
# self.setFixedSize(1000,800)
self.home()
def home(self):
btn = QtGui.QPushButton("Physics", self)
btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
btn.resize(100, 100)
btn.resize(100, 100)
# btn.move(width/2,height/2)
self.show()
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()
Assuming that what you want is that the button stays in the middle of the window always, that you could do it by overwriting the resizeEvent method.
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 500, 300)
self.setWindowTitle("SciCalc")
self.setWindowIcon(QtGui.QIcon('atom.png'))
self.home()
def home(self):
self.btn = QtGui.QPushButton("Physics", self)
self.btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
self.btn.resize(100, 100)
self.show()
def resizeEvent(self, event):
self.btn.move(self.rect().center()-self.btn.rect().center())
QtGui.QMainWindow.resizeEvent(self, event)
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()

PyQt4 - Open *.py file when Button is clicked

I want to open/run a *.py file with pythonw.exe, when the Start-Button is clicked. Can anyone tell me how this works? I havent found the right function anywhere.
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
self.setToolTip('This is a <b>QWidget</b> widget')
btn1 = QtGui.QPushButton('Start', self)
# OPENFILE SOMEHOW!!
btn1.resize(btn1.sizeHint())
btn1.move(20, 20)
qbtn = QtGui.QPushButton('Quit', self)
qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
qbtn.resize(qbtn.sizeHint())
qbtn.move(150, 20)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Python Script')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You can use subprocess.call. For example, this code runs external.py , when the Start is clicked:
import sys
from PyQt4 import QtGui, QtCore
import subprocess
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def run(self, path):
subprocess.call(['pythonw',path])
def initUI(self):
QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
self.setToolTip('This is a <b>QWidget</b> widget')
btn1 = QtGui.QPushButton('Start', self)
btn1.resize(btn1.sizeHint())
btn1.move(20, 20)
btn1.clicked.connect(lambda:self.run('external.py'))
qbtn = QtGui.QPushButton('Quit', self)
qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
qbtn.resize(qbtn.sizeHint())
qbtn.move(150, 20)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Python Script')
#subprocess.call(['pythonw','3.py'])
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

How can I make a white box in PyQt?

I have a PyQt application that has a widget called Deck
class Deck(QtGui.QWidget):
def __init__(self, filename, parent):
super(Deck, self).__init__(parent)
self.setMinimumSize(100, 150)
self.setStyleSheet('background-color: white;')
label = QtGui.QLabel("deck", self)
label.show()
I expected the Deck widget to be all white, but it is only under the label, although it accepts clicks on its 100x150 area and adjusts its hbox:s size.
Edit:
The surrounding layout.
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
#import qt4reactor
#qt4reactor.install()
from deck import Deck
class Duel(QtGui.QWidget):
def __init__(self):
super(Duel, self).__init__()
topArea = QtGui.QHBoxLayout()
topArea.addStretch(1)
d = Deck(sys.argv[1], self)
d.show()
topArea.addWidget(d)
bottomArea = QtGui.QHBoxLayout()
d = Deck(sys.argv[2], self)
d.show()
bottomArea.addWidget(d)
bottomArea.addStretch(1)
vbox = QtGui.QVBoxLayout()
vbox.addLayout(topArea)
vbox.addStretch(1)
vbox.addLayout(bottomArea)
self.setLayout(vbox)
def main():
root = Duel()
root.show()
app.exec_()
if __name__ == '__main__':
main()

Categories