I have tried triggered, actionTriggered and many more and yet it always throws an attribute error, like 'NoneType' object has no attribute 'actionTriggered'. I use a .UI file that i created in QTDesigner.
from PyQt5.QtWidgets import QMainWindow, QApplication, QToolBar, QAction
from PyQt5 import uic
import sys
class UI(QMainWindow):
def __init__(self):
super(UI,self).__init__()
uic.loadUi("TTTT.ui",self)
self.show()
self.tbar=self.findChild(QToolBar,"actionfff")
self.tbar.actionTriggered.connect(self.T)
def T(self):
print("mmmmm")
app = QApplication(sys.argv)
UIWindow = UI()
app.exec_()
It's a test program, because I thought I messed up in the original code.
Any help will be mutch appreciated!
Related
I followed a GUI course but the exact code does not work for me. I was trying to be guided to the next page when I clicked the button. But every time I click it, Python stops working with:
Process finished with exit code -1073740791 (0xC0000409)
Here's the code:
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
loadUi("screen1.ui", self)
self.button.clicked.connect(self.gotoScreen2)
def gotoScreen2(self):
widget.setCurrentIndex(widget.currentWidget()+1)
class Screen2(QMainWindow):
def __init__(self):
super(Screen2, self).__init__()
loadUi("screen2.ui",self)
self.button2.clicked.connect(self.gotoScreen1)
def gotoScreen1(self):
widget.setCurrentIndex(widget.currentWidget()+1)
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
mainwindow = MainWindow()
widget.addWidget(mainwindow)
screen2 = Screen2()
widget.addWidget(screen2)
widget.setFixedWidth(600)
widget.setFixedWidth(800)
widget.show()
try:
sys.exit(app.exec())
except:
print("Exiting..")
There may be some errors in code. It's my first time working on a GUI.
I have changed the
widget.setCurrentIndex(widget.currentWidget()+1)
to
widget.setCurrentIndex(1)
and it solved my problem. Now I can switch between pages without any crashes.
You have to install a module named 'PyQt5'
I'm creating a basic pyqt6 application using the QMainWindow Class.
My code is basic. I create a basic window, but whenever I execute this code, I do not see any menu:
from PyQt6.QtWidgets import QStatusBar, QApplication, QWidget, QMainWindow, QVBoxLayout, QScrollBar, \
QToolButton
import sys
# There are THREE different window type classes
# that we can choose from:
# QWidget, QMainWindow, QDialog
class Window(QMainWindow):
def __init__(self):
super().__init__()
# Manages GUI Applications Control Flow
# and main settings..
app = QApplication([])
window = Window()
window.setWindowTitle("My 1st App")
window.statusBar().showMessage("Status Bar Message")
window.menuBar().addMenu("Menu 1")
window.show()
sys.exit(app.exec())
What am I doing wrong?
Why isn't "MENU 1" showing in the GUI?
import sys
import requests
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication
class MainWindow(QDialog):
def __init__(self):
super(MainWindow, self).__init__()
loadUi("tabletutorial.ui",self)
self.request_function()
def request_function(self):
for i in range(0,100):
resp=requests.get("https://www.google.com")
print(resp.status_code)
# main
app = QApplication(sys.argv)
mainwindow = MainWindow()
widget = QtWidgets.QStackedWidget()
widget.addWidget(mainwindow)
widget.setFixedHeight(850)
widget.setFixedWidth(1120)
widget.show()
try:
sys.exit(app.exec_())
except:
print("Exiting")
This window is created by another main window.
Now the problem is when i quit the pyqt5 window the script is still running and i am getting the status code .I am running a big application with a bunch of requests.Anyone with relatable solution please ?
I TRIED:
self.close() not worked for me. #QtCore.pyqtSlot() also not worked.
I am new to here . Please ignore mistakes and kind answer are appriciated.
I would like to add a QCheckBox to a QFileDialog. I would like to use the static method QFileDialog.getSaveFileName() to show the dialog.
I have found several similar questions, all in c++:
How to add checkbox to QFileDialog window in QT3?
Adding a widget in QFileDialog
https://www.qtcentre.org/threads/42858-Creating-a-Custom-FileOpen-Dialog
https://forum.qt.io/topic/103964/add-checkbox-to-qfiledialog/7
I did my best to translate these discussions to python, but have not gotten to the solution yet. My code runs, but the checkbox does not show up, even when I use QFileDialog.DontUseNativeDialog.
This is how I am subclassing QFileDialog:
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtWidgets import QCheckBox
class ChkBxFileDialog(QFileDialog):
def __init__(self, chkBxTitle=""):
super().__init__()
self.setOption(QFileDialog.DontUseNativeDialog)
chkBx = QCheckBox(chkBxTitle)
self.layout().addWidget(chkBx)
#end __init__
#end ChkBxFileDialog
I have run this in two ways.
Option 1 (with extra QFileDialog.DontUseNativeDialog):
import sys
from PyQt5.QtWidgets import QApplication
if __name__ == "__main__":
app = QApplication(sys.argv)
fileDialog = ChkBxFileDialog(chkBxTitle="Chkbx")
fileName = fileDialog.getSaveFileName(filter='*.txt', initialFilter='*.txt',
options=QFileDialog.DontUseNativeDialog)[0]
sys.exit(app.exec_())
Option 2 (without extra QFileDialog.DontUseNativeDialog):
import sys
from PyQt5.QtWidgets import QApplication
if __name__ == "__main__":
app = QApplication(sys.argv)
fileDialog = ChkBxFileDialog(chkBxTitle="Chkbx")
fileName = fileDialog.getSaveFileName(filter='*.txt', initialFilter='*.txt')[0]
sys.exit(app.exec_())
The checkbox doesn't show with either option. Option 1 uses different window styling. Option 2 shows the typical PyQt QFileDialog.
Does anyone know what I am missing?
The problem is that getSaveFileName is a static method so they do not inherit from ChkBxFileDialog and therefore do not have the custom behavior.
There are 2 options:
Don't use getSaveFileName but implement the logic using QFileDialog directly:
import sys
from PyQt5.QtWidgets import QApplication, QCheckBox, QDialog, QFileDialog
class ChkBxFileDialog(QFileDialog):
def __init__(self, chkBxTitle="", filter="*.txt"):
super().__init__(filter=filter)
self.setSupportedSchemes(["file"])
self.setOption(QFileDialog.DontUseNativeDialog)
self.setAcceptMode(QFileDialog.AcceptSave)
self.selectNameFilter("*.txt")
chkBx = QCheckBox(chkBxTitle)
self.layout().addWidget(chkBx)
def main():
app = QApplication(sys.argv)
dialog = ChkBxFileDialog()
if dialog.exec_() == QDialog.Accepted:
filename = dialog.selectedUrls()[0].toLocalFile()
print(filename)
if __name__ == "__main__":
main()
Use some trick to get the QFileDialog instance, such as getting all the topLevels and verifying that it is a QFileDialog.
import sys
from functools import partial
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QCheckBox, QDialog, QFileDialog
def add_checkbox(chkBxTitle):
for tl in QApplication.topLevelWidgets():
if isinstance(tl, QFileDialog):
tl.setOption(QFileDialog.DontUseNativeDialog)
chkBx = QCheckBox(chkBxTitle)
tl.layout().addWidget(chkBx)
def main():
app = QApplication(sys.argv)
QTimer.singleShot(1000, partial(add_checkbox, ""))
fileName, _ = QFileDialog.getSaveFileName(
filter="*.txt", initialFilter="*.txt", options=QFileDialog.DontUseNativeDialog
)
if __name__ == "__main__":
main()
I would like to use the static method QFileDialog.getSaveFileName() to show the dialog
That's not possible. The static method as is defined in the C++ code knows nothing about your derived class, so it will create an instance of the base class, which doesn't contain your modifications. You have to explicitly instantiate your derived class, call exec() on the instance, check the return code and possibly call its selectedFiles() method to see what files were selected.
I have a strange problem, hope someone can clear it for me
import os
from os import path
import sys
import pathlib
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget,
QWizard, QWizardPage, QLineEdit, \
QTabWidget, QApplication,
QTextEdit,QToolTip,QPushButton,QMessageBox
from PyQt5.QtCore import QSize,pyqtSlot,pyqtProperty
from PyQt5.QtGui import QFont
from PyQt5.uic import loadUiType
app = QApplication(sys.argv)
if getattr(sys, 'frozen', False):
# we are running in a bundle
installPath = sys._MEIPASS
print('we are running in a bundle')
else:
# we are running in a normal Python environment
installPath = os.path.dirname(os.path.abspath(__file__))
print('we are running in a normal Python environment')
UI_File, _ = loadUiType(path.join(path.dirname(__file__), 'test.ui'))
class MainAPP(QTabWidget, UI_File):
def __init__(self, parent=None):
super(MainAPP, self).__init__(parent)
self.setupUi(self)
self.handle_buttons()
def handle_buttons(self):
self.pushButton.clicked.connect(self.test_2)
def test_2(self):
for i in range(10):
self.listWidget.addItem(str('lklk'))
self.listWidget.itemClicked.connect(self.test)
def test(self):
for i in range(10):
self.listWidget_2.addItem(str('DDD'))
self.listWidget_2.itemClicked.connect(self.test_3)
def test_3(self):
print ('hi')
def main():
app = QApplication(sys.argv)
main = MainAPP()
main.show()
app.exec_()
if __name__ == "__main__":
main()
so basically, I have a push button, if I click on it it will display some data at listWidget and if I clicked on any item in listWidget , it will display other data on ListWidget_2 and then if I click on item in List_widget_2 it then should print ('Hi')
the problem is if I click multiple times in ListWidget and then click on an item in ListWidget_2 , I received more than one ('Hi) , it will diplay ('Hi') according to the number of clicks I clicked in the Listwidget
any idea what could be the issue
You only need to make a connection between a signal and a slot once. Currently you are making additional connections each time you click an item in the first list widget, which results in your method printing "hi" executing once for every connection you made.
To fix this, make both of the signal connections either in the test_2 method or in the __init__ method