I'm trying to test if a shortcut is working using PyQt5 and QTest. Here is my code:
Main.py
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication, QPushButton
class Window(QWidget):
def __init__(self):
super().__init__()
self.more_btn = QPushButton("More")
self.more_btn.clicked.connect(self.on_clicked)
self.more_btn.setShortcut(QKeySequence.Undo)
vbox = QVBoxLayout()
vbox.addWidget(self.more_btn)
self.setLayout(vbox)
def on_clicked(self):
print("Item clicked")
test_main.py
import sys
import unittest
from PyQt5.QtGui import QKeySequence
from PyQt5.QtTest import QTest
from PyQt5.QtWidgets import QApplication
from Main import Window
class MainTestCase(unittest.TestCase):
def test_main(self):
app = QApplication(sys.argv)
form = Window()
QTest.keySequence(form, QKeySequence.Undo)
When I run test_main, item_clicked should be printed, but it doesn't. I tried to debug the program and set the breatpoint at the print("Item clicked") line, it didn't stop. It seems that QTest.keySequence didn't work. Why? How should I make it work?
Related
An application is being created with a large number of different windows.
The bottom line is that I am trying to get from the main window (by clicking button) to the client window. There is "Back" button in the clients window, which should return the user to the main window.
Both codes are in different files. The problem occurs at the stage of pressing the "Customers" button (Process finished with exit code -1073740791 (0xC0000409)).
Moreover, if everything is in one file, then everything works fine. But only here the program is not planned to be small, so I don't want to put a huge code in one file.
Welcome_Screen.py
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QDialog, QApplication
import Customers
class WelcomeScreen(QDialog):
def __init__(self):
super(WelcomeScreen, self).__init__()
loadUi('screens/welcomescreen.ui', self)
self.CustomerData.clicked.connect(self.go_to_CustomerData)
def go_to_CustomerData(self):
client = Customers.CustomerScreen()
widget.addWidget(client)
widget.setCurrentIndex(widget.currentIndex() + 1)
# main
app = QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('Icons/MainIcon.png'))
app.setApplicationDisplayName('Bonus Program')
welcome = WelcomeScreen()
widget = QtWidgets.QStackedWidget()
widget.addWidget(welcome)
widget.setFixedWidth(600)
widget.setFixedHeight(475)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")
Customers.py
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QDialog, QApplication, QWidget, QMessageBox, QTableWidget, QTableWidgetItem, QTableView
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel, QSqlQuery
from PyQt5.QtGui import QIcon
import Welcome_Screen as ws
import sqlite3
db_path = 'Bonus_db.sqlite'
class CustomerScreen(QDialog):
def __init__(self):
super(CustomerScreen, self).__init__()
loadUi("screens/customerscreen.ui", self)
self.back_btn.setIcon(QIcon('Icons/Back.png'))
self.back_btn.setIconSize(QtCore.QSize(45, 60))
self.back_btn.clicked.connect(self.go_Back_to_WelcomeScreen)
# self.SoldTos.clicked.connect(self.gotoSoldTos)
def go_Back_to_WelcomeScreen(self):
welcome = ws.WelcomeScreen()
ws.widget.addWidget(welcome)
ws.widget.setCurrentIndex(ws.widget.currentIndex() + 1)
Customer_app = QApplication(sys.argv)
cust_window = CustomerScreen()
cust_window.show()
sys.exit(Customer_app.exec_())
I made an joystick with qframe class and also i converted my mouse location data to -1,1 but when i try to send this location data to my mainwindow class with pyqt signal it gives an error 'pyqt signal has no attiribute emit' like this, i go to pyqtSignal class
here it is it really dont have emit function but there are many example like this on the net,does my pyqt files broken or something or is it because of absance of QObject class? How can i pass my data to other class?
Here it is my joystick class;
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5.QtCore import QPointF, QTimer, Qt ,pyqtSignal
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtWidgets import QFrame, QApplication, QLabel
import sys
import rospy
from sensor_msgs.msg import Joy
class clock(QFrame):
def __init__(self):
super(clock,self).__init__()
self.setMinimumSize(300,300)
self.setMaximumSize(300,300)
self.x=self.width()/2
self.y=self.height()/2
self.relative_x=0
self.relative_y=0
self.signal=pyqtSignal(int)
def sub(self):
self.node=rospy.init_node("hanınamk",anonymous=True)
rospy.Subscriber('/joy',Joy,self.joyControl)
def joyControl(self,data):
self.data=data.axes
self.x=(-(self.data[0]*150)+150)
self.y=(-(self.data[1]*150)+150)
print(self.data[0],self.data[1])
self.repaint()
def paintEvent(self,event):
pen=QPen()
pen.setWidth(5)
pen.setBrush(Qt.black)
painter=QPainter(self)
painter.setPen(pen)
painter.setRenderHint(QPainter.Antialiasing)
painter.drawEllipse(self.rect())
painter_2=QPainter(self)
painter_2.setBrush(Qt.black)
painter_2.setRenderHint(QPainter.Antialiasing)
painter_2.drawEllipse(QPointF(self.x,self.y),50,50)
def mouseMoveEvent(self,e):
self.relative_x=(e.x()-150)/150.0
self.relative_y=(e.y()-150)/150.0
# print(round(self.relative_x,1),round(self.relative_y,1))
self.signal.emit(self.relative_x)
if self.relative_x**2+self.relative_y**2<1:
self.x=e.x()
self.y=e.y()
self.repaint()
def mousePressEvent(self,e):
if self.relative_x**2+self.relative_y**2<1:
self.x=e.x()
self.y=e.y()
self.repaint()
def mousePressEvent(self,e):
if self.relative_x**2+self.relative_y**2<1:
self.x=e.x()
self.y=e.y()
self.repaint()
def mouseReleaseEvent(self,e):
self.x=self.width()/2
self.y=self.height()/2
self.repaint()
if __name__=="__main__":
app=QApplication(sys.argv)
win=clock()
win.show()
sys.exit(app.exec_())
and my main class;
from PyQt5.QtCore import QRect, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QMouseEvent
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
import sys
from joystick import clock
class Main(QWidget):
def __init__(self):
super(Main,self).__init__()
self.setWindowTitle('Main')
self.setMinimumSize(900,900)
self.setMaximumSize(900,900)
layout=QHBoxLayout()
layout.setGeometry(QRect(200,200,300,300))
self.joy=clock()
self.joy_2=clock()
self.setLayout(layout)
layout.addWidget(self.joy)
layout.addWidget(self.joy_2)
self.label=QLabel(self)
#pyqtSlot(int)
def on_change(self,data):
print(data)
if __name__=="__main__":
app=QApplication(sys.argv)
win=Main()
win.show()
sys.exit(app.exec_())
Make the signal a class attribute not an instance attribute:
class clock(QFrame):
signal = pyqtSignal(int)
def __init__(self):
# ...
and connect it in Main.__init__:
self.joy.signal.connect(self.on_change)
self.joy2.signal.connect(self.on_change)
I use Qtdesigner and then convert it to py file..I want to take input text through the QLineEdit and put it to pyautogui.typewrite. facing problem to solve it.
I write the the code bleow
import pyautogui
import time
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QMovie
from PyQt5.QtCore import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUiType
from messageRHR import Ui_MainWindow
class MainThread(QThread):
def __init__(self,message):
super(MainThread,self).__init__()
# self.message = QLineEdit(self)
def run(self):
self.taskExecution()
def taskExecution(self):
pyautogui.typewrite(message)
time.sleep(1)
pyautogui.press('Enter')
startExecution = MainThread()
class Main(QMainWindow):
def __init__(self):
super(Main, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.startTask)
self.ui.pushButton_2.clicked.connect(self.close)
def startTask(self):
self.ui.movie = QtGui.QMovie("00545cb7179c504433d4c8f5e845f286.gif")
self.ui.label_2.setMovie(self.ui.movie)
self.ui.movie.start()
self.ui.movie = QtGui.QMovie("00545cb7179c504433d4c8f5e845f286.gif")
self.ui.label_3.setMovie(self.ui.movie)
self.ui.movie.start()
message = QLineEdit(self)
startExecution.start()
app = QApplication(sys.argv)
rsn = Main()
rsn.show()
exit(app.exec_())
I'm designing window with PyQt5 and QtDesigner. I made maindemo.py, maindemo.ui, mainfail.py, mainfail.ui.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QGraphicsView, QLabel, QMenuBar, QMenu, QStatusBar, QAction, qApp, QMessageBox
from PyQt5 import uic, QtCore
form_class = uic.loadUiType("maindemo.ui")[0]
class OpeningWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.initUI()
def initUI(self):
self.setWindowTitle('Qomics')
self.btn_survival.setToolTip('Survival Analysis')
self.btn_drug.setToolTip('Drug Analysis')
self.btn_CRISPR.setToolTip('CRISPR Analysis')
self.btn_cellline.setToolTip('Cell Line')
self.btn_survival.clicked.connect(self.open_SurvivalMainWindow)
self.btn_drug.clicked.connect(self.open_DrugWindow)
self.btn_CRISPR.clicked.connect(self.open_sgRNAWindow)
self.btn_cellline.clicked.connect(self.open_CellLineWindow)
actionExit = QAction('&Exit', self)
actionExit.setShortcut('Ctrl+Q')
actionExit.setStatusTip('Exit Application')
actionExit.triggered.connect(qApp.quit)
self.statusBar().showMessage('abcd')
self.setGeometry(200, 100, 800, 530)
self.show()
def openSurvivalMainWindow(self):
open_SurvivalMainWindow = SurvivalMainWindow()
open_SurvivalMainWindow.show()
def openDrugWindow(self):
open_DrugWindow = DrugWindow()
open_DrugWindow.show()
def opensgRNAWindow(self):
open_sgRNAWindow = sgRNAWindow()
open_sgRNAWindow.show()
def openCellLineWindow(self):
open_CellLineWindow = scatterWindow()
open_CellLineWindow.show()
above code is maindemo.py
what I want to do is clicking btn_drug, btn_sgRNA, btn_cellline connects to new window(with mainfail.py, mainfail.ui)
Only btn_survival connects to the real function and other buttons connects to mainfail window.
I tried to use if, else... but I couldn't write proper code..
I wrote a code but it doesn't work.
if openSurvivalMainWindow():
else:
openMainFailWindow.show()
Not clear what you want, but here an example of a multi window
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QGraphicsView, QLabel, QMenuBar, QMenu, QStatusBar, QAction, qApp, QMessageBox
from PyQt5 import uic, QtCore
from PyQt5.uic import loadUiType
login, _ = loadUiType('login.ui')
registration,_ = loadUiType('registration.ui')
class Register(QMainWindow, registration):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
self.pushButton_24.clicked.connect(self.adding_users)
self.pushButton_25.clicked.connect(self.return_login)
def return_login(self):
self.window2 = Login()
self.close()
self.window2.show()
class Login(QWidget, login):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.Handel_Login)
self.pushButton_2.clicked.connect(self.registrations)
def registrations(self):
self.window2 = Register()
self.close()
self.window2.show()
######
#....
######
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle('Fusion')
window = Login()
#window = MainApp()
window.show()
sys.exit(app.exec_())
With this your application will go first to the login page and if you push the button 2 you will go to the registration page and of course the log in page will be closed
I was writing a pyqt5 program for expression evaluator but after running the program i am not able to see any widgets and getting blank window
def expressionevaluator():
import sys
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget,QMainWindow
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Tutorial")
self.setWindowIcon=QtGui.QIcon('pyqt_example2.PNG')
self.home()
def ExitForm(self):
sys.exit()
def home(self):
vbox=QtWidgets.QVBoxLayout()
textbrowser=QtWidgets.QTextBrowser()
lineedit=QtWidgets.QLineEdit()
btn=QtWidgets.QPushButton("QUIT")
btn.clicked.connect(self.close)
vbox.addWidget(textbrowser)
vbox.addWidget(lineedit)
vbox.addWidget(btn)
self.setLayout(vbox)
self.show()
if __name__=="__main__":
app=QApplication(sys.argv)
GUI=Window()
sys.exit(app.exec_())
expressionevaluator()
So what should I do ?
Just running your code I got a widget showing up in my screen, but its components didn't show up. Instead of setting a layout of a QMainWindow try to have a central widget (QWidget) set its layout with its components than set the QMainWindow central widget with this widget. There you go, now you have all working fine.
You had problems with the layout because QMainWindow behaves differently from others Widgets, it has its own layout and many other default behaviors, central widget is the reason why nothing was showing up inside your main window.
def expressionevaluator():
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QTextBrowser
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
class Window(QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Tutorial")
self.setWindowIcon = QIcon('pyqt_example2.PNG')
self.home()
def ExitForm(self):
sys.exit()
def home(self):
vbox = QVBoxLayout()
textbrowser = QTextBrowser()
lineedit = QLineEdit()
btn = QPushButton("QUIT")
central_widget = QWidget()
central_widget.setLayout(vbox)
btn.clicked.connect(self.close)
vbox.addWidget(textbrowser)
vbox.addWidget(lineedit)
vbox.addWidget(btn)
self.setCentralWidget(central_widget)
self.show()
if __name__=="__main__":
app = QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())
expressionevaluator()
Note: There are many improvements in the structure of your code you could do, I just changed as less as I could to make it work, for example try to not import all the modules at once, import just what you need for example QIcon, QLineEdit and so on, instead of the whole QtWidgets, or QtCore...
Following code works well:
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QTextBrowser
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
class Window(QMainWindow):
def __init__(self):
super(Window,self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt Tutorial")
self.setWindowIcon = QIcon('pyqt_example2.PNG')
self.home()
def ExitForm(self):
sys.exit()
def home(self):
vbox = QVBoxLayout()
textbrowser = QTextBrowser()
lineedit = QLineEdit()
btn = QPushButton("QUIT")
central_widget = QWidget()
central_widget.setLayout(vbox)
btn.clicked.connect(self.ExitForm)
vbox.addWidget(textbrowser)
vbox.addWidget(lineedit)
vbox.addWidget(btn)
self.setCentralWidget(central_widget)
self.show()
if __name__=="__main__":
app = QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())