Why pushButton don't connect? - python-3.x

When i'm closing window, pushButtons don't connecting
I want window to be open, when first is closed
But pushButtons don't connecting
It is test.py
import sys
import sqlite3
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QMainWindow
from easter import Ui_Form
from window_3 import App_2
class App(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Test")
self.show()
def closeEvent(self, event):
Example().__init__()
class Example(QMainWindow, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
# It is don't working
self.buttonGroup.buttonClicked.connect(self.add)
self.show()
def add(self, sender):
# It doesn't matter
con = sqlite3.connect("top.db")
cur = con.cursor()
idd = list(cur.execute("select id from Top").fetchall())
a = cur.execute('update top set score = ? where id = ?', (int(sender.text()), idd[-1][0]))
con.commit()
App_2().__init__()
self.close()

Related

Outputting text in a form with flow using signals

Need help with the following task:
There is a main window MainWindow and there is a separate class of a stream in which there is a text. Tell me how to use signals on the plainTextEdit form, which is located on the main form, to display text through signals?
Main window code:
import sys
from PySide6.QtWidgets import *
from Controller.Potok_Controller import Potok_Controller
from View.ui_potok import Ui_MainWindow
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.open_window)
def open_window(self):
self.myThread = Potok_Controller()
# self.myThread.mysignal.connect(self.sendText)
self.myThread.start()
if __name__ == '__main__':
app = QApplication()
window = MainWindow()
window.show()
sys.exit(app.exec())
UI code of the main window:
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'untitledkonQbZ.ui'
##
## Created by: Qt User Interface Compiler version 6.3.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QFrame, QMainWindow, QPlainTextEdit,
QPushButton, QSizePolicy, QVBoxLayout, QWidget)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.verticalLayout = QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(u"verticalLayout")
self.frame = QFrame(self.centralwidget)
self.frame.setObjectName(u"frame")
self.frame.setFrameShape(QFrame.StyledPanel)
self.frame.setFrameShadow(QFrame.Raised)
self.verticalLayout_2 = QVBoxLayout(self.frame)
self.verticalLayout_2.setObjectName(u"verticalLayout_2")
self.plainTextEdit = QPlainTextEdit(self.frame)
self.plainTextEdit.setObjectName(u"plainTextEdit")
self.verticalLayout_2.addWidget(self.plainTextEdit)
self.verticalLayout.addWidget(self.frame)
self.frame_2 = QFrame(self.centralwidget)
self.frame_2.setObjectName(u"frame_2")
self.frame_2.setFrameShape(QFrame.StyledPanel)
self.frame_2.setFrameShadow(QFrame.Raised)
self.verticalLayout_3 = QVBoxLayout(self.frame_2)
self.verticalLayout_3.setObjectName(u"verticalLayout_3")
self.pushButton = QPushButton(self.frame_2)
self.pushButton.setObjectName(u"pushButton")
self.verticalLayout_3.addWidget(self.pushButton)
self.verticalLayout.addWidget(self.frame_2)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.pushButton.setText(QCoreApplication.translate("MainWindow", u"\u0421\u0442\u0430\u0440\u0442 \u043f\u043e\u0442\u043e\u043a\u0430", None))
# retranslateUi
Thread class code:
import time
from PySide6.QtCore import QThread
class Potok_Controller(QThread):
def __init__(self, parent=None):
super(Potok_Controller, self).__init__(parent)
def txt(self):
txt1 = 'test1'
time.sleep(1)
txt2 = 'test2'
time.sleep(1)
txt3 = 'test3'
def run(self):
self.txt()
You just need to implement the sendtext method that you have commented out in your open_window method, and then create a signal that accepts a string parameter in your Potok_Controller class and emit the signal from the threads run method.
For Example:
class Potok_Controller(QThread):
mysignal = Signal([str]) # create signal
def __init__(self, parent=None):
super(Potok_Controller, self).__init__(parent)
def txt(self):
txt1 = 'test1'
time.sleep(1)
txt2 = 'test2'
time.sleep(1)
txt3 = 'test3'
return txt1 + txt2 + txt3
def run(self):
self.mysignal.emit(self.txt()) # emit the signal with text as parameter
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.open_window)
def sendText(self, text): # create the sendText method
self.ui.plaintextedit.setPlainText(text)
def open_window(self):
self.myThread = Potok_Controller()
self.myThread.mysignal.connect(self.sendText) # uncomment this
self.myThread.start()

Disable mouse pointer in QGraphicsView

I want to disable the mouse pointer in a QGraphicsView.
What line of code do I need to add in the following example?
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGraphicsView
class GraphicsWindow(QGraphicsView):
def __init__(self, parent=None):
super().__init__(parent)
self.showFullScreen()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
graphics_window = GraphicsWindow()
graphics_window.show()
sys.exit(app.exec_())
Qt::BlankCursor A blank/invisible cursor, typically used when the cursor shape needs to be hidden.
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGraphicsView
class GraphicsWindow(QGraphicsView):
def __init__(self, parent=None):
super().__init__(parent)
self.showFullScreen()
self.setCursor(Qt.BlankCursor) # < ------
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
graphics_window = GraphicsWindow()
graphics_window.show()
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_())

Screen functions in PYQT

import sys
from PyQt4 import QtCore, QtGui, uic
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import time
import datetime
import re
import random
import csv
from CropClass import *
from Wheat_class import *
from Potato_class import *
win1 = uic.loadUiType("MenuScreen.ui")[0]
win2 = uic.loadUiType("WheatScreen.ui")[0]
win3 = uic.loadUiType("PotatoScreen.ui") [0]
class MenuScreen(QtGui.QMainWindow, win1):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.BtnCreateSimulation.clicked.connect(self.changeSimulation)
def changeSimulation(self):
if self.RdoWheat.isChecked() ==True:
print("Wheat is picked")
new_crop=Wheat()
self.wheatSimulation()
elif self.RdoPotato.isChecked() == True:
print("Potato is picked")
new_crop = Potato()
self.PotatoSimulation()
def wheatSimulation(self):
print("Hello")
self.hide()
WheatWindow.show()
def PotatoSimulation(self):
self.hide()
PotatoWindow.show()
class PotatoScreen(QtGui.QMainWindow, win3):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.BtnBacktoMenu.clicked.connect(self.BackToMain)
def BackToMain(self):
self.hide()
MenuWindow.show()
class WheatScreen(QtGui.QMainWindow, win2):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
new_crop = Wheat()
self.BtnBacktoMenu.clicked.connect(self.BackToMain)
self.BtnManual.clicked.connect(self.ManualCalculate)
def BackToMain(self):
self.hide()
MenuWindow.show()
def ManualCalculate(self):
water = self.spBoxWater.value()
light= self.spBoxLight.value()
print("water", water, "light", light)
def main():
app = QtGui.QApplication(sys.argv)
WheatWindow = WheatScreen(None)
PotatoWindow = PotatoScreen(None)
MenuWindow = MenuScreen(None)
MenuWindow.show()
app.exec_()
if __name__ == "__main__":
main()
I have created a program in Python which simulates the growth rates of crops. The user is able to chose between the crop is wheat or potatoes I am trying to create a GUI using PYQT. The problem I am having is that when I try and load the program the program is not recognizing the other screen layouts. The main function should be setting up the screen layouts

Back to Previous Window

i'm new in Python and PyQt4.
I want to ask "How to back to previous Window ?"
i have 2 file in here, file 'login' and 'signup'
here file login.py
import sys
from PyQt4.QtGui import QWidget, QPushButton, QLineEdit, QLabel, \
QApplication, QGridLayout
from signup import SignUp
class Login(QWidget):
def __init__(self):
super(Login, self).__init__()
self.setWindowTitle("Login")
self.login_window()
def login_window(self):
self.login_layout = QGridLayout()
self.login_button = QPushButton("Login")
self.signup_button = QPushButton("Sign Up")
self.login_layout.addWidget(self.login_button, 2, 0)
self.login_layout.addWidget(self.signup_button, 2, 1)
self.signup_button.clicked.connect(self.signup_show)
self.setLayout(self.login_layout)
self.show()
def signup_show(self):
self.signupshow = SignUp()
self.hide()
self.signupshow.show()
def check_signup(self):
SignUp.check_signup()
self.show()
def main():
app = QApplication(sys.argv)
login = Login()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
and here is signup.py
import sys
from PyQt4.QtGui import QWidget, QPushButton, QLineEdit, QLabel, \
QApplication, QGridLayout
class SignUp(QWidget):
def __init__(self):
super(SignUp, self).__init__()
self.setWindowTitle("Sign Up")
self.signup_window()
def signup_window(self):
self.signup_layout = QGridLayout()
self.signup_button = QPushButton("Sign Up")
self.signup_layout.addWidget(self.signup_button, 2, 0, 1, 0)
self.signup_button.clicked.connect(self.check_signup)
self.setLayout(self.signup_layout)
self.show()
def check_signup(self):
self.close()
def main():
app = QApplication(sys.argv)
signup = SignUp()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
my problem is, when i push button signup from signup.py, it's close but window Login is not show.
i think i don't have any trigger in signup.py for check_signup in login.py
in this code, i delete some Line Edit and Label. I think it's not a problem.
i hope someone will help, Thank you before :)
and if you answer this questtion, i hope you will explain a little bit of the logic, thank you again :)
I'm not really sure what you are trying to achieve (your description is kind of confusing), but let's start here. I put all your code in one file, since you need to import each other (which leads to cyclic import). Then, I just added these two lines
self.login = Login()
self.login.show()
into check_signup method of SignUp class, which pops-up Login window. If this is not your desired result, please let us know and provide us a better description. The code follows:
import sys
from PyQt4.QtGui import QWidget, QPushButton, QLineEdit, QLabel, \
QApplication, QGridLayout
class Login(QWidget):
def __init__(self):
super(Login, self).__init__()
self.setWindowTitle("Login")
self.login_window()
def login_window(self):
self.login_layout = QGridLayout()
self.login_button = QPushButton("Login")
self.signup_button = QPushButton("Sign Up")
self.login_layout.addWidget(self.login_button, 2, 0)
self.login_layout.addWidget(self.signup_button, 2, 1)
self.signup_button.clicked.connect(self.signup_show)
self.setLayout(self.login_layout)
self.show()
def signup_show(self):
self.signupshow = SignUp()
self.hide()
self.signupshow.show()
def check_signup(self):
SignUp.check_signup()
self.show()
class SignUp(QWidget):
def __init__(self):
super(SignUp, self).__init__()
self.setWindowTitle("Sign Up")
self.signup_window()
def signup_window(self):
self.signup_layout = QGridLayout()
self.signup_button = QPushButton("Sign Up")
self.signup_layout.addWidget(self.signup_button, 2, 0, 1, 0)
self.signup_button.clicked.connect(self.check_signup)
self.setLayout(self.signup_layout)
self.show()
def check_signup(self):
self.login = Login()
self.login.show()
self.close()
def main():
app = QApplication(sys.argv)
signup = SignUp()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You should rather use signal-slot connections:
import sys
from PyQt4 import QtGui, QtCore
class WidgetA(QtGui.QWidget):
open_b = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(WidgetA, self).__init__(parent)
self.resize(100, 100)
self.l = QtGui.QVBoxLayout()
self.close_btn = QtGui.QPushButton('Close')
self.b_btn = QtGui.QPushButton('Open B')
self.b_btn.clicked.connect(self.b_btn_clicked)
self.l.addWidget(self.close_btn)
self.l.addWidget(self.b_btn)
self.setLayout(self.l)
def b_btn_clicked(self):
self.open_b.emit()
self.hide()
class WidgetB(QtGui.QWidget):
open_a = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(WidgetB, self).__init__(parent)
self.resize(100, 100)
self.l = QtGui.QVBoxLayout()
self.close_btn = QtGui.QPushButton('Close')
self.close_btn.clicked.connect(self.hide)
self.a_btn = QtGui.QPushButton('Open A')
self.a_btn.clicked.connect(self.a_btn_clicked)
self.l.addWidget(self.close_btn)
self.l.addWidget(self.a_btn)
self.setLayout(self.l)
def a_btn_clicked(self):
self.open_a.emit()
self.hide()
def main():
app = QtGui.QApplication(sys.argv)
a = WidgetA()
b = WidgetB()
a.open_b.connect(lambda: b.show())
b.open_a.connect(lambda: a.show())
a.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

Resources