how would I make the openFileDialog open txt file in this case - python

I am trying to have
fname = QFileDialog(self,'Source Text','','txt(*.txt)')
run when you call the function openFileDialog
I have tried using a lambda instead of a normal function but that didn't work ethier
I am trying to make a cryptography program and am setting up the basic text upload system
I am currently trying to set fname to the file name
import sys
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QDialog, QApplication, QFileDialog
from tkinter import filedialog
def Encrypt(SourceText):
print (SourceText)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(886, 487)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.Enter = QtWidgets.QPushButton(self.centralwidget)
self.Enter.clicked.connect(self.clicked)
self.Enter.setObjectName("Enter")
self.gridLayout.addWidget(self.Enter, 2, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setLayoutDirection(QtCore.Qt.LayoutDirection.RightToLeft)
self.label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setObjectName("plainTextEdit")
self.gridLayout.addWidget(self.plainTextEdit, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 886, 20))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionUpload = QtGui.QAction(MainWindow)
self.actionUpload.setShortcutContext(
QtCore.Qt.ShortcutContext.WindowShortcut)
self.actionUpload.setMenuRole(QtGui.QAction.MenuRole.AboutQtRole)
self.actionUpload.setObjectName("actionUpload")
self.menuFile.addAction(self.actionUpload)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.actionUpload.triggered.connect(lambda: self.Open("File Uploaded"))
def clicked(self):
Encrypt(self.plainTextEdit.toPlainText())
def openFileDialog(self):
fname = QFileDialog(self,'Source Text','','txt(*.txt)')
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.Enter.setText(_translate("MainWindow", "Enter"))
self.label.setText(_translate(
"MainWindow", "Enter Text Below Or Upload File"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionUpload.setText(_translate("MainWindow", "Upload"))
self.actionUpload.setShortcut(_translate("MainWindow","Ctrl+O"))
def Open(self, text):
openFileDialog()
print (text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec())

You must use QFileDialog().getOpenFileName(parent, caption, path, filter) instead of QFileDialog(parent, caption, path, filter):
import sys
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QDialog, QApplication, QFileDialog
from tkinter import filedialog
def Encrypt(SourceText):
print(SourceText)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(886, 487)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.Enter = QtWidgets.QPushButton(self.centralwidget)
self.Enter.clicked.connect(self.clicked)
self.Enter.setObjectName("Enter")
self.gridLayout.addWidget(self.Enter, 2, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setLayoutDirection(QtCore.Qt.LayoutDirection.RightToLeft)
self.label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setObjectName("plainTextEdit")
self.gridLayout.addWidget(self.plainTextEdit, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 886, 20))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionUpload = QtGui.QAction(MainWindow)
self.actionUpload.setShortcutContext(
QtCore.Qt.ShortcutContext.WindowShortcut)
self.actionUpload.setMenuRole(QtGui.QAction.MenuRole.AboutQtRole)
self.actionUpload.setObjectName("actionUpload")
self.menuFile.addAction(self.actionUpload)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.actionUpload.triggered.connect(lambda: self.Open("File Uploaded"))
def clicked(self):
Encrypt(self.plainTextEdit.toPlainText())
def openFileDialog(self):
fname, _ = QFileDialog().getOpenFileName(self, 'Source Text', '', 'Text files (*.txt)')
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.Enter.setText(_translate("MainWindow", "Enter"))
self.label.setText(_translate(
"MainWindow", "Enter Text Below Or Upload File"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionUpload.setText(_translate("MainWindow", "Upload"))
self.actionUpload.setShortcut(_translate("MainWindow", "Ctrl+O"))
def Open(self, text):
self.openFileDialog()
print(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec())

Related

argument 1 has unexpected type 'EnumMeta'

I am trying to display a simple GUI
# Form implementation generated from reading ui file 'UI/form.ui'
# Created by: PyQt6 UI code generator 6.1.0
# WARNING: Any manual changes made to this file will be lost when pyuic6
# is run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(886, 487)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.Enter = QtWidgets.QPushButton(self.centralwidget)
self.Enter.setObjectName("Enter")
self.gridLayout.addWidget(self.Enter, 2, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setLayoutDirection(QtCore.Qt.LayoutDirection.RightToLeft)
self.label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setObjectName("plainTextEdit")
self.gridLayout.addWidget(self.plainTextEdit, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 886, 20))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionUpload = QtGui.QAction(MainWindow)
self.actionUpload.setShortcutContext(QtCore.Qt.WindowShortcut)
self.actionUpload.setMenuRole(QtGui.QAction.AboutQtRole)
self.actionUpload.setObjectName("actionUpload")
self.menuFile.addAction(self.actionUpload)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.Enter.setText(_translate("MainWindow", "Enter"))
self.label.setText(_translate("MainWindow", "Enter Text Below Or Upload File"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionUpload.setText(_translate("MainWindow", "Upload"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec())
but I get
Exception has occurred: TypeError
I have tried using pyuic6 again and to no avail
I am using PyQt6 and am running a Debian Linux based OS
If this code is generated by pyuic then it is a PyQt bug. In PyQt6 the flags and enumerations are stricter so you must change
self.actionUpload.setShortcutContext(QtCore.Qt.WindowShortcut)
self.actionUpload.setMenuRole(QtGui.QAction.AboutQtRole)
to:
self.actionUpload.setShortcutContext(QtCore.Qt.ShortcutContext.WindowShortcut)
self.actionUpload.setMenuRole(QtGui.QAction.MenuRole.AboutQtRole)

Msg_box after clicking button in PyQT

I want to get MsgBox after button click. I try to use PyQt5 for it. I make windows form with one button:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(320, 330, 251, 51))
self.pushButton.setObjectName("PushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Then I try to enter button clicked function:
def buttonClicked(self):
msg = QMessageBox()
msg.setText("This is a message box")
But it doesn't work at all. How should I spent my problem?
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(320, 330, 251, 51))
self.pushButton.setObjectName("PushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.buttonClicked) # +++
def buttonClicked(self):
msg = QtWidgets.QMessageBox.information(
self,
'Information',
'This is a message box')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
You have done well to create the QMessageBox but not shown them, so a possible solution is to use the exec_() method:
def buttonClicked(self):
msg = QMessageBox()
msg.setText("This is a message box")
msg.exec_()

I'm trying to make my PyQt GUI code in OOP way

I'm trying to make my PyQt5 GUI in OOP way for example class for window and another for Pushbutton I'm really confiuse how to make it.Here is my code and my try to make pushbutton class.also my goal is to make classes for each of the items in the window
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
#making Pushbutton
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 210, 93, 28))
self.pushButton.setStyleSheet("QPushButton::setCheckable(bool)")
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
# here I'm trying to make Buttom class
class Button(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Button, self).__init__(parent)
# Main function
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
from Subclassing of QPushButton :
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 6 16:21:24 2020
#author: Pietro
"""
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
#making Pushbutton
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(180, 210, 93, 28))
self.pushButton.setStyleSheet("QPushButton::setCheckable(bool)")
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pippo=CustomButtonClass(self.centralwidget)
self.pippo.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
# here I'm trying to make Buttom class
class CustomButtonClass(QtWidgets.QPushButton):
def __init__(self, *args, **kwargs):
QtWidgets.QPushButton.__init__(self,*args, **kwargs)
self.setGeometry(QtCore.QRect(280, 310, 93, 28))
self.setText('customized')
self.hide()
self.setStyleSheet("QPushButton{\n"
" background-color: #9de650;\n"
"}\n"
"\n"
"\n"
"QPushButton:hover{\n"
" background-color: green;\n"
"}\n"
"\n"
"")
# Main function
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

How to connect 3 different programs in one main file using pyqt5?

I have 3 programs here, and I have a problem in running those 3 programs because I want it to be all imported to the main file and connected to each other after clicking the buttons on each window. My problem here is that after I have reached the 2nd window, when I click the button there then it doesn't proceed to the last window but instead the window crashes. What would I change in my program so that it will not crash?
s1.py (main program)
from PyQt5 import QtCore, QtGui, QtWidgets
from s4 import *
from s3 import *
class Ui_MainWindow(object):
def ok (self):
self.window=QtWidgets.QMainWindow()
self.ui=Ui_MainWindow2()
self.ui.setup(self.window)
self.window.show()
MainWindow.hide()
def proceed (self):
self.window1=QtWidgets.QMainWindow()
self.ui1=Ui_MainWindow3()
self.ui1.setup1(self.window1)
self.window1.show()
MainWindow2.hide()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(330, 220, 93, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.ok)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "ok"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
s4.py (2nd program)
from PyQt5 import QtCore, QtGui, QtWidgets
from s3 import *
class Ui_MainWindow2(object):
def proceed (self):
self.window1=QtWidgets.QMainWindow()
self.ui1=Ui_MainWindow3()
self.ui1.setup1(self.window1)
self.window1.show()
MainWindow2.hide()
def setup(self, MainWindow2):
MainWindow2.setObjectName("MainWindow2")
MainWindow2.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow2)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(320, 270, 93, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.proceed)
MainWindow2.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow2)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow2.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow2)
self.statusbar.setObjectName("statusbar")
MainWindow2.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow2)
QtCore.QMetaObject.connectSlotsByName(MainWindow2)
def retranslateUi(self, MainWindow2):
_translate = QtCore.QCoreApplication.translate
MainWindow2.setWindowTitle(_translate("MainWindow2", "MainWindow"))
self.pushButton.setText(_translate("MainWindow2", "PROCEED"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow2 = QtWidgets.QMainWindow()
ui = Ui_MainWindow2()
ui.setup(MainWindow2)
MainWindow2.show()
sys.exit(app.exec_())
s3.py(3rd program)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow3(object):
def setup1(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(260, 190, 451, 181))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "THIS IS MY LAST WINDOW"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow3()
ui.setup1(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets
from s4 import Ui_MainWindow2
class Ui_MainWindow(object):
def ok (self):
self.window=QtWidgets.QMainWindow()
self.ui=Ui_MainWindow2()
self.ui.setup(self.window)
self.window.show()
MainWindow.hide()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(330, 220, 93, 28))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.ok)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "ok"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
and import Ui_MainWindow3 from s3.py into s4 file and call proceed function as well as:
from s3 import Ui_MainWindow3

Getting a blank form when running PyQt application

I wrote a small PyQt program based on an example from a book. I edited the code to add some niceties. Below is my code:
from __future__ import division
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class Ui_MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(9, 9, 781, 461))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setObjectName("verticalLayout")
self.textBrowser = QtWidgets.QTextBrowser(self.verticalLayoutWidget)
self.textBrowser.setObjectName("textBrowser")
self.verticalLayout.addWidget(self.textBrowser)
self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 480, 781, 80))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
self.horizontalLayout_2.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.horizontalLayoutWidget)
self.lineEdit.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setPointSize(12)
self.lineEdit.setFont(font)
self.lineEdit.setText("")
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_2.addWidget(self.lineEdit)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.actionQuit = QtWidgets.QAction(MainWindow)
self.actionQuit.setObjectName("actionQuit")
self.menuFile.addAction(self.actionQuit)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Expression:"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionQuit.setText(_translate("MainWindow", "Quit (Ctrl+Q)"))
self.actionQuit.setShortcut(_translate("MainWindow", "Ctrl+Q"))
self.lineEdit.setFocus()
self.connect(self.lineEdit, self.updateUi)
self.setWindowTitle("Calculate")
def updateUi(self):
try:
text = unicode(self.lineEdit.text())
self.browser.append("%s = <b>%s</b>" % (text,eval(text)))
except:
self.browser.append("<font color=red>Invalid Expression</font>")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Ui_MainWindow()
window.show()
sys.exit(app.exec_())
Output:
But when I execute this file I only get a blank window without any widgets inside it. What is the error in my code?
You defined setupUi() function but I don't see you executing it anywhere. You should expand your __init__ with:
class Ui_MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setupUi(your_main_window)

Categories

Resources