Updating/clearing the PyQt form using confirmation window - python-3.x

I would like that by typing anything in LineEdit in the first window and by pressing 'Confirm clearing' button in the second window, the first window would be updated so that the LineEdit field is emptied and focused.
import sys
from PyQt6.QtWidgets import (
QApplication, QWidget, QPushButton, QLabel, QLineEdit, QGridLayout
)
class Vw(QWidget):
def __init__(self):
super().__init__()
self.resize(1000, 400)
self.setContentsMargins(30, 30, 30, 30)
self.grid = QGridLayout(self)
self.setLayout(self.grid)
self.t1 = QLineEdit(self)
self.grid.addWidget(self.t1, 0, 1)
self.bOk = QPushButton("Clear the line edit", self)
self.bOk.setFixedSize(120, 30)
self.grid.addWidget(self.bOk, 5, 0)
self.bOk.clicked.connect(self.implementing)
self.bOk.setAutoDefault(True)
def implementing(self):
# to be added a code
done()
def clearing(self):
self.t1.clear()
self.t1.setFocus()
def done():
Vw.wda = WdA()
Vw.wda.show()
class WdA(QWidget):
def __init__(self):
super().__init__()
self.resize(300, 50)
self.setContentsMargins(0, 0, 0, 0)
self.grid = QGridLayout(self)
self.setLayout(self.grid)
self.el1 = QLabel("About to clear!", self)
self.grid.addWidget(self.el1, 0, 0)
self.wOk = QPushButton("Confirm clearing", self)
self.wOk.setFixedSize(120, 30)
self.grid.addWidget(self.wOk, 1, 0)
self.wOk.setFocus()
self.wOk.clicked.connect(self.end_of_entry)
self.wOk.setAutoDefault(True)
def end_of_entry(self):
self.close()
# Vw.clearing() # <--- This line needs to be implemented
def appl():
app_ = QApplication(sys.argv)
wnd = Vw()
wnd.show()
sys.exit(app_.exec())
if __name__ == '__main__':
appl()
In other words, I would like 'clearing' method would trigger on clicking on 'wOk' button.
In other words, I would like 'clearing' method would trigger on clicking on 'wOk' button.

As an option using Signals & Slots.
Support for Signals and Slots
import sys
'''
from PyQt6.QtWidgets import (
QApplication, QWidget, QPushButton, QLabel, QLineEdit, QGridLayout
)
'''
from PyQt5.QtWidgets import (
QApplication, QWidget, QPushButton, QLabel, QLineEdit, QGridLayout
)
from PyQt5.QtCore import pyqtSignal # <---
class WdA(QWidget):
signal = pyqtSignal() # <---
def __init__(self):
super().__init__()
self.resize(300, 50)
self.setContentsMargins(0, 0, 0, 0)
self.grid = QGridLayout(self)
self.setLayout(self.grid)
self.el1 = QLabel("About to clear!", self)
self.grid.addWidget(self.el1, 0, 0)
self.wOk = QPushButton("Confirm clearing", self)
self.wOk.setFixedSize(120, 30)
self.grid.addWidget(self.wOk, 1, 0)
self.wOk.setFocus()
self.wOk.clicked.connect(self.end_of_entry)
self.wOk.setAutoDefault(True)
def end_of_entry(self):
self.signal.emit() # <---
self.close()
class Vw(QWidget):
def __init__(self):
super().__init__()
self.resize(1000, 400)
self.setContentsMargins(30, 30, 30, 30)
self.grid = QGridLayout(self)
self.setLayout(self.grid)
self.t1 = QLineEdit(self)
self.grid.addWidget(self.t1, 0, 1)
self.bOk = QPushButton("Clear the line edit", self)
self.bOk.setFixedSize(120, 30)
self.grid.addWidget(self.bOk, 5, 0)
self.bOk.clicked.connect(self.implementing)
self.bOk.setAutoDefault(True)
self.wda = WdA() # +++
self.wda.signal.connect(self.clearing) # <---
def implementing(self):
self.wda.show()
def clearing(self): # <---
self.t1.clear()
self.t1.setFocus()
def closeEvent(self, event):
self.wda.close()
def appl():
app_ = QApplication(sys.argv)
wnd = Vw()
wnd.show()
sys.exit(app_.exec())
if __name__ == '__main__':
appl()

Related

how to make a slider which has a label can show above the handle with pyside2

The current value could displayed immediately above the slider,such like this:
enter image description here
Thanks so much!
I had try like it but it show not good enough.
Here is my codes:
import sys
from PySide2.QtWidgets import QApplication, QWidget, QSlider, QLabel
from PySide2.QtCore import Qt
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.slider = QSlider(Qt.Horizontal, self)
self.slider.setRange(0, 100)
self.slider.setValue(50)
self.slider.setGeometry(30, 40, 100, 30)
self.slider.valueChanged[int].connect(self.changeValue)
self.label = QLabel(self)
self.label.setText("50")
self.label.setGeometry(140, 40, 30, 30)
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('QSlider')
self.show()
def changeValue(self, value):
self.label.setText(str(value))
self.label.move(self.slider.x() + value, self.slider.y() - 20)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWidget()
sys.exit(app.exec_())

How can I take a value from a line edit to another window's line edit by using Python and PyQt5?

What I need:
I need to create a simple project that can take value from window to another window
My research effort:
So, I create two classes for two windows then connect to classes with each other, so when I click in button it takes the value from 1st window then open the other window but the value=nothing because the clickMethod returns nothing.
Below is my code:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(320, 140))
self.setWindowTitle("First Window")
self.nameLabel = QLabel(self)
self.nameLabel.setText('1st:')
self.line = QLineEdit(self)
self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)
pybutton = QPushButton('OK', self)
pybutton.clicked.connect(self.second_wind) #connect button to open second window
pybutton.clicked.connect(self.clickMethod)
pybutton.resize(200,32)
pybutton.move(80, 60)
def clickMethod(self):
value =self.line.text() #take value from the line edit
return value
def second_wind(self): #object from secod_window class
self.SW = Second_Window()
self.SW.show()
class Second_Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(320, 140))
self.setWindowTitle("Second Window")
self.nameLabel = QLabel(self)
self.nameLabel.setText('Name:')
self.line = QLineEdit(self)
self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)
self.f = MainWindow() #make object from MainWindow class to execute clickMethod() to reutrn value
a=self.f.clickMethod()
print(a)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )
I expect the Clickmethod to return the value
but it returns nothing
Try it:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(320, 140))
self.setWindowTitle("First Window")
self.nameLabel = QLabel(self)
self.nameLabel.setText('1st:')
self.line = QLineEdit(self)
self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)
pybutton = QPushButton('OK', self)
pybutton.clicked.connect(self.second_wind)
# pybutton.clicked.connect(self.clickMethod)
pybutton.resize(200,32)
pybutton.move(80, 60)
# def clickMethod(self):
# value =self.line.text()
# return value
def second_wind(self):
text = self.line.text() # +++
self.SW = Second_Window(text) # +++ (text)
self.SW.show()
class Second_Window(QMainWindow):
def __init__(self, text): # +++ (text)
QMainWindow.__init__(self)
self.text = text # +
self.setMinimumSize(QSize(320, 140))
self.setWindowTitle("Second Window")
self.nameLabel = QLabel(self)
self.nameLabel.setText('Name:')
self.line = QLineEdit(self)
self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)
# self.f = MainWindow()
# a=self.f.clickMethod()
self.line.setText(self.text) # +
print(self.text)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )

QTimer execute function faster by one second when I pause then resume

Trying to build a stopwatch GUI and I am unable to make the pause function to work. Every time when I pause the timer and then resume, the timer runs faster by a second.
import sys
import datetime
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QGridLayout
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QFont, QPalette
class StopWatch(QWidget):
def __init__(self):
super().__init__()
self.isRunning = False
self.counter = 0
self.setWindowTitle('Stop Watch')
self.create_layout()
def create_layout(self):
self.timer = QTimer()
self.timer.setInterval(1000)
labelFormating = QPalette()
labelFormating.setColor(QPalette.WindowText, Qt.darkBlue)
self.startButton = QPushButton('Start')
self.pauseButton = QPushButton('Pause')
self.stopButton = QPushButton('Stop')
self.startButton.setEnabled(True)
self.pauseButton.setEnabled(False)
self.stopButton.setEnabled(False)
self.startButton.clicked.connect(self.start_timer)
self.pauseButton.clicked.connect(self.pause)
self.stopButton.clicked.connect(self.stop_timer)
self.displayLabel = QLabel(str(datetime.timedelta(seconds=0)))
self.displayLabel.setFont(QFont('Open Sans', 24))
self.displayLabel.setPalette(labelFormating)
gridLayout = QGridLayout()
gridLayout.addWidget(self.displayLabel, 0, 0, 1, 3) # row span | color span (extend)
gridLayout.addWidget(self.startButton, 1, 0, 1, 1)
gridLayout.addWidget(self.pauseButton, 1, 1, 1, 1)
gridLayout.addWidget(self.stopButton, 1, 2, 1, 1)
gridLayout.setAlignment(self.displayLabel, Qt.AlignCenter)
self.setLayout(gridLayout)
def start_timer(self, slot):
print('start clicked')
self.startButton.setEnabled(False)
self.pauseButton.setEnabled(True)
self.stopButton.setEnabled(True)
self.timer.start()
self.timer.timeout.connect(self.run) #This signal is emitted when the timer times out.
def stop_timer(self):
print('Stop clicked')
self.startButton.setEnabled(True)
self.pauseButton.setEnabled(False)
self.stopButton.setEnabled(False)
self.isRunning = False
self.timer.stop()
self.displayLabel.setText(str(datetime.timedelta(seconds=0)))
def run(self):
# print('Counter value', str(self.counter))
self.counter += 1
self.display()
def pause(self):
print('pause clicked')
self.startButton.setEnabled(True)
self.pauseButton.setEnabled(False)
self.stopButton.setEnabled(True)
self.timer.stop()
self.displayLabel.setText(str(datetime.timedelta(seconds=self.counter)))
def display(self):
# print(str(datetime.timedelta(seconds=self.counter)))
self.displayLabel.setText(str(datetime.timedelta(seconds=self.counter)))
def main():
app = QApplication(sys.argv)
window = StopWatch()
window.show()
sys.exit(app.exec_())
main()
What I am looking for is to perform the pause function. For example, when I click Start, then Pause, then Start, I wish the counter to run normally. Any help is appreciated.
Try it:
import sys
import datetime
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QGridLayout
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QFont, QPalette
class StopWatch(QWidget):
def __init__(self):
super().__init__()
self.isRunning = False
self.counter = 0
self.setWindowTitle('Stop Watch')
self.create_layout()
def create_layout(self):
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.run) # +++
labelFormating = QPalette()
labelFormating.setColor(QPalette.WindowText, Qt.darkBlue)
self.startButton = QPushButton('Start')
self.pauseButton = QPushButton('Pause')
self.stopButton = QPushButton('Stop')
self.startButton.setEnabled(True)
self.pauseButton.setEnabled(False)
self.stopButton.setEnabled(False)
self.startButton.clicked.connect(self.start_timer)
self.pauseButton.clicked.connect(self.pause)
self.stopButton.clicked.connect(self.stop_timer)
self.displayLabel = QLabel(str(datetime.timedelta(seconds=0)))
self.displayLabel.setFont(QFont('Open Sans', 24))
self.displayLabel.setPalette(labelFormating)
gridLayout = QGridLayout()
gridLayout.addWidget(self.displayLabel, 0, 0, 1, 3)
gridLayout.addWidget(self.startButton, 1, 0, 1, 1)
gridLayout.addWidget(self.pauseButton, 1, 1, 1, 1)
gridLayout.addWidget(self.stopButton, 1, 2, 1, 1)
gridLayout.setAlignment(self.displayLabel, Qt.AlignCenter)
self.setLayout(gridLayout)
def start_timer(self, slot):
self.startButton.setEnabled(False)
self.pauseButton.setEnabled(True)
self.stopButton.setEnabled(True)
self.timer.start()
# self.timer.timeout.connect(self.run) # ----
def stop_timer(self):
self.startButton.setEnabled(True)
self.pauseButton.setEnabled(False)
self.stopButton.setEnabled(False)
self.isRunning = False
self.timer.stop()
# ? self.displayLabel.setText(str(datetime.timedelta(seconds=0)))
self.counter = 0 # +++
def run(self):
self.counter += 1
self.display()
def pause(self):
self.startButton.setEnabled(True)
self.pauseButton.setEnabled(False)
self.stopButton.setEnabled(True)
self.timer.stop()
# self.displayLabel.setText(str(datetime.timedelta(seconds=self.counter)))
def display(self):
self.displayLabel.setText(str(datetime.timedelta(seconds=self.counter)))
def main():
app = QApplication(sys.argv)
window = StopWatch()
window.show()
sys.exit(app.exec_())
main()

pyqt5 trying to use QGridLayout to organise my QLabel, QLineEdit, QPushButton, and "Pop-up" QLabel

I am trying to get the "Game Name:" (QLabel), input box (QLineEdit), and QPushButton on one line and the "Pop-up" QLabel) to appear on the bottom
but am having difficulties with get QGridLayout to work
With this code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QLabel, QLineEdit, QGridLayout, QGroupBox, QDialog
from PyQt5.QtCore import pyqtSlot
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("Project PiBu!!")
self.createGridLayout()
self.windowLayout = QVBoxLayout()
self.windowLayout.addWidget(self.horizontalGroupBox)
self.setLayout(self.windowLayout)
self.game_name = QLabel("Game Name:", self)
self.game_line_edit = QLineEdit(self)
self.search_button = QPushButton("Search", self)
self.search_button.clicked.connect(self.on_click)
self.game = QLabel(self)
self.show()
def createGridLayout(self):
self.horizontalGroupBox = QGroupBox()
self.layout = QGridLayout()
self.layout.setColumnStretch(1, 4)
self.layout.setColumnStretch(2, 4)
self.layout.addWidget(self.game_name, 0, 0)
self.layout.addWidget(self.game_line_edit, 0, 1)
self.layout.addWidget(self.search_button, 0, 2)
self.layout.addWidget(self.game, 1, 0)
self.horizontalGroupBox.setLayout(layout)
#pyqtSlot()
def on_click(self):
self.game.setText(self.game_line_edit.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
I am getting this error:
AttributeError: 'Window' object has no attribute 'game_name'
Why?
have a feeling its something simple rather than something more complicated but maybe I'm wrong
Please help!!!
Thank you!
You call the createGridLayout method earlier than you define the variables used in it.
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QHBoxLayout,
QVBoxLayout, QPushButton, QLabel, QLineEdit,
QGridLayout, QGroupBox, QDialog)
from PyQt5.QtCore import pyqtSlot
class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("Project PiBu!!")
# self.createGridLayout()
# self.windowLayout = QVBoxLayout()
# self.windowLayout.addWidget(self.horizontalGroupBox)
# self.setLayout(self.windowLayout)
self.game_name = QLabel("Game Name:", self)
self.game_line_edit = QLineEdit(self)
self.search_button = QPushButton("Search", self)
self.search_button.clicked.connect(self.on_click)
self.game = QLabel(self)
self.createGridLayout() # < --
self.windowLayout = QVBoxLayout() # < --
self.windowLayout.addWidget(self.horizontalGroupBox) # < --
self.setLayout(self.windowLayout) # < --
self.show()
def createGridLayout(self):
self.horizontalGroupBox = QGroupBox()
self.layout = QGridLayout()
self.layout.setColumnStretch(1, 4)
self.layout.setColumnStretch(2, 4)
# AttributeError: 'Window' object has no attribute 'game_name'
self.layout.addWidget(self.game_name, 0, 0)
self.layout.addWidget(self.game_line_edit, 0, 1)
self.layout.addWidget(self.search_button, 0, 2)
self.layout.addWidget(self.game, 1, 0)
self.horizontalGroupBox.setLayout(self.layout) # +++ self.
#pyqtSlot()
def on_click(self):
self.game.setText(self.game_line_edit.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())

Spin box not showing up

I'm unsure why all aspects of my GUI are showing up apart from the spin box (the code for it is within the home function).
I've tried moving it to the init(self) function, but that doesn't work. I thought it would be intuitive for it to be within the home function as that is where all my other GUI (e.g. buttons) resides.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QAction, QMessageBox, QDoubleSpinBox
from temperature import MplWindow
from filament import MplWindow1
from highvoltage import MplWindow2
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(50, 50, 300, 300)
self.setWindowTitle('Temperature Control')
self.setWindowIcon(QIcon('adn.png'))
extractAction = QAction('&Quit', self)
extractAction.setShortcut('Ctrl+Q')
extractAction.setStatusTip('leave the app')
extractAction.triggered.connect(self.close_application)
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(extractAction)
self.matplWindow = MplWindow()
self.matplWindow1 = MplWindow1()
self.matplWindow2 = MplWindow2()
self.home()
def home(self):
btn = QPushButton('quit', self)
btn.clicked.connect(self.close_application)
btn.resize(btn.sizeHint())
btn.move(200, 260)
button = QPushButton('Temperature',self)
button.clicked.connect(self.opengraph)
button.move(100,50)
button = QPushButton('Filament voltage',self)
button.clicked.connect(self.openfilament)
button.move(100,80)
button = QPushButton('High voltage',self)
button.clicked.connect(self.openhigh)
button.move(100,110)
self.doubleSpinBox = QtWidgets.QDoubleSpinBox()
self.doubleSpinBox.setGeometry(180, 110, 62, 22)
self.show()
def opengraph(self):
self.matplWindow.funAnimation()
def openfilament(self):
self.matplWindow1.funAnimation1()
def openhigh(self):
self.matplWindow2.funAnimation2()
def close_application(self):
choice = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if choice == QMessageBox.Yes:
sys.exit()
else:
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
Gui = window()
sys.exit(app.exec_())
I worked it out - I moved the code to the init function.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QAction, QMessageBox, QDoubleSpinBox, QLabel, QVBoxLayout
from temperature import MplWindow # +++
from filament import MplWindow1
from highvoltage import MplWindow2
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(50, 50, 300, 300)
self.setWindowTitle('Temperature Control')
self.setWindowIcon(QIcon('adn.png'))
extractAction = QAction('&Quit', self)
extractAction.setShortcut('Ctrl+Q')
extractAction.setStatusTip('leave the app')
extractAction.triggered.connect(self.close_application)
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(extractAction)
self.matplWindow = MplWindow() # +++
self.matplWindow1 = MplWindow1()
self.matplWindow2 = MplWindow2()
# vBoxLayout = QVBoxLayout()
self.label = QLabel("Set point Temp:", self)
self.label.move(50,150)
self.spinBox = QDoubleSpinBox(self)
self.spinBox.move(70,150)
self.home()
def home(self):
btn = QPushButton('quit', self)
btn.clicked.connect(self.close_application)
btn.resize(btn.sizeHint())
btn.move(200, 260)
button = QPushButton('Temperature',self)
button.clicked.connect(self.opengraph)
button.move(100,50)
button = QPushButton('Filament voltage',self)
button.clicked.connect(self.openfilament)
button.move(100,80)
button = QPushButton('High voltage',self)
button.clicked.connect(self.openhigh)
button.move(100,110)
self.show()
def opengraph(self):
self.matplWindow.funAnimation() # +++
def openfilament(self):
self.matplWindow1.funAnimation1()
def openhigh(self):
self.matplWindow2.funAnimation2()
def close_application(self):
choice = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if choice == QMessageBox.Yes:
sys.exit()
else:
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
Gui = window()
sys.exit(app.exec_())

Resources