I am a Structural Engineer by trade and I am trying to automate the creation of 3D models using scripts.
So far I have created three modules; the GUI module using PyQt4, a main module that controls the signals from the GUI, and an export module which "should" pull the variables from main module and generate a script that can be read by my analysis program.
So far the I can't pull the variables from main module when clicking the export menu in the GUI because variable names are not defined.
If I import the main module into the export module to get the variables, I get errors with the Ui_MainWindow.
I have tried to simplify what I am doing below.
main.py module
import sys
from PyQt4 import QtGui, QtCore
from gui import Ui_MainWindow
from export import newFile
class Main(QtGui.QMainWindow):
def __init__(self):
super(Main, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setName()
self.ui.actionExport.triggered.connect(self.exportName)
def exportName(self):
self.exportStaad = newFile().createNewFile()
def setName(self):
self.ui.tbo_Name.textChanged.connect(self.name_Changed)
def name_Changed(self):
someName = self.ui.tbo_Name.text()
print('Name = ' + someName)
app = QtGui.QApplication(sys.argv)
form = Main()
form.show()
app.exec_()
gui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'gui.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tbo_Name = QtGui.QLineEdit(self.centralwidget)
self.tbo_Name.setGeometry(QtCore.QRect(80, 60, 150, 20))
self.tbo_Name.setObjectName(_fromUtf8("tbo_Name"))
self.lab_Name = QtGui.QLabel(self.centralwidget)
self.lab_Name.setGeometry(QtCore.QRect(30, 60, 40, 20))
self.lab_Name.setObjectName(_fromUtf8("lab_Name"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuFile = QtGui.QMenu(self.menubar)
self.menuFile.setObjectName(_fromUtf8("menuFile"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.actionExport = QtGui.QAction(MainWindow)
self.actionExport.setObjectName(_fromUtf8("actionExport"))
self.menuFile.addAction(self.actionExport)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.lab_Name.setText(_translate("MainWindow", "Name:", None))
self.menuFile.setTitle(_translate("MainWindow", "File", None))
self.actionExport.setText(_translate("MainWindow", "Export", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
export.py
import sys
from PyQt4 import QtGui, QtCore
from os import path
import math
class newFile():
def createNewFile(dest):
'''
Creates file
'''
name = QtGui.QFileDialog.getSaveFileName ()
f = open(name, 'w')
f.write('Hello' + someName)
f.close
The method called createNewFile(dest) inside the class newFile uses undefined var someName at f.write('Hello' + someName).
This causes the error as it is not defined in the class. Define a variable before you use it.
Related
I've created a MainWindow design called Ui_Dashboard within Qt Designer. I've also created a widget called "units_table", which I'd like to import and display within the Ui_Dashboard.
I've created a new class and inherited the Ui_Dashboard class, but for some reason cannot access the verticalLayout object. See code below:
dashboard.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'dashboard.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Dashboard(object):
def setupUi(self, Dashboard):
Dashboard.setObjectName(_fromUtf8("Dashboard"))
Dashboard.resize(800, 600)
self.centralwidget = QtGui.QWidget(Dashboard)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.widget = QtGui.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(50, 21, 683, 360))
self.widget.setObjectName(_fromUtf8("widget"))
self.verticalLayout = QtGui.QVBoxLayout(self.widget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.units_button = QtGui.QPushButton(self.widget)
self.units_button.setCheckable(True)
self.units_button.setChecked(False)
self.units_button.setObjectName(_fromUtf8("units_button"))
self.horizontalLayout.addWidget(self.units_button)
self.calls_button = QtGui.QPushButton(self.widget)
self.calls_button.setCheckable(True)
self.calls_button.setObjectName(_fromUtf8("calls_button"))
self.horizontalLayout.addWidget(self.calls_button)
self.vehicles_button = QtGui.QPushButton(self.widget)
self.vehicles_button.setCheckable(True)
self.vehicles_button.setObjectName(_fromUtf8("vehicles_button"))
self.horizontalLayout.addWidget(self.vehicles_button)
self.persons_button = QtGui.QPushButton(self.widget)
self.persons_button.setCheckable(True)
self.persons_button.setObjectName(_fromUtf8("persons_button"))
self.horizontalLayout.addWidget(self.persons_button)
self.pushButton_6 = QtGui.QPushButton(self.widget)
self.pushButton_6.setCheckable(True)
self.pushButton_6.setObjectName(_fromUtf8("pushButton_6"))
self.horizontalLayout.addWidget(self.pushButton_6)
self.pushButton_4 = QtGui.QPushButton(self.widget)
self.pushButton_4.setCheckable(True)
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
self.horizontalLayout.addWidget(self.pushButton_4)
self.pushButton_5 = QtGui.QPushButton(self.widget)
self.pushButton_5.setCheckable(True)
self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
self.horizontalLayout.addWidget(self.pushButton_5)
self.verticalLayout.addLayout(self.horizontalLayout)
self.inserted_module = QtGui.QWidget(self.widget)
self.inserted_module.setMinimumSize(QtCore.QSize(481, 321))
self.inserted_module.setObjectName(_fromUtf8("inserted_module"))
self.verticalLayout.addWidget(self.inserted_module)
Dashboard.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(Dashboard)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName(_fromUtf8("menubar"))
Dashboard.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(Dashboard)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
Dashboard.setStatusBar(self.statusbar)
self.retranslateUi(Dashboard)
QtCore.QMetaObject.connectSlotsByName(Dashboard)
def retranslateUi(self, Dashboard):
Dashboard.setWindowTitle(_translate("Dashboard", "MainWindow", None))
self.units_button.setText(_translate("Dashboard", "Units", None))
self.calls_button.setText(_translate("Dashboard", "Calls", None))
self.vehicles_button.setText(_translate("Dashboard", "Vehicles", None))
self.persons_button.setText(_translate("Dashboard", "Persons", None))
self.pushButton_6.setText(_translate("Dashboard", "PushButton", None))
self.pushButton_4.setText(_translate("Dashboard", "PushButton", None))
self.pushButton_5.setText(_translate("Dashboard", "PushButton", None))
main.py
import sys
from PyQt4 import QtCore, QtGui, uic
from dashboard import Ui_Dashboard
class MainWindow(QtGui.QMainWindow, Ui_Dashboard):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.units_table = uic.loadUi('units_table.ui', self)
self.inserted_module = self.units_table
self.inserted_module.setMinimumSize(QtCore.QSize(481, 321))
self.verticalLayout.addWidget(self.inserted_module)
self.setupUi(self)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
I'm able to display the imported widget perfectly fine, but I need to add it as a child to the vertical layout, which is where I'm having difficulties. Despite Ui_Dashboard being inherited, I still can't access the verticalLayout.
I keep getting the following error:
AttributeError: 'MainWindow' object has no attribute 'verticalLayout'
verticalLayout does not exist because it is created in the setupUi() method, what you must do is load it before you want to use it. Another mistake is that you should not pass self to loadUi(), since what you will do is reimplement the widget's design:
import sys
from PyQt4 import QtCore, QtGui, uic
from dashboard import Ui_Dashboard
class MainWindow(QtGui.QMainWindow, Ui_Dashboard):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.units_table = uic.loadUi('units_table.ui')
self.verticalLayout.addWidget(self.units_table )
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
I'm having a problem where when I save the text from QTextEdit as a txt, or rtf, it doesnt save things like underline and font size. Here's the code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
import gi
import signal
gi.require_version('Gtk', '3.0')
import sys
import dbus
import pygtk
import gi
import signal
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import pyqtSlot
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
gi.require_version('Notify', '0.7')
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(679, 600)
self.underlined = False
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.saveButton = QtGui.QPushButton(self.centralwidget)
self.saveButton.setGeometry(QtCore.QRect(10, 0, 88, 28))
self.saveButton.setObjectName(_fromUtf8("pushButton"))
self.textEdit = QtGui.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(0, 30, 681, 800))
self.textEdit.setObjectName(_fromUtf8("textEdit"))
self.fontButton = QtGui.QPushButton(self.centralwidget)
self.fontButton.setGeometry(QtCore.QRect(400, 0, 88, 28))
self.fontButton.setObjectName(_fromUtf8("fontButton"))
self.fontSize = QtGui.QLineEdit(self.centralwidget)
self.fontSize.setGeometry(QtCore.QRect(100, 0, 28, 28))
self.fontSize.setObjectName(_fromUtf8("fontEdit"))
self.fontSize.returnPressed.connect(self.fontButton.click)
self.underlineButton = QtGui.QPushButton(self.centralwidget)
self.underlineButton.setGeometry(QtCore.QRect(130, 0, 28, 28))
self.underlineButton.setObjectName(_fromUtf8("underlineButton"))
self.disableUnderlineButton = QtGui.QPushButton(self.centralwidget)
self.disableUnderlineButton.setGeometry(QtCore.QRect(160, 0, 28, 28))
self.disableUnderlineButton.setObjectName(_fromUtf8("disableUnderlineButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 679, 24))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuTest = QtGui.QMenu(self.menubar)
self.menuTest.setObjectName(_fromUtf8("menuTest"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuTest.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def save(self):
with open('log.rtf', 'w') as yourFile:
yourFile.write(str(self.textEdit.toPlainText()))
def saveFont(self):
self.textEdit.setFontPointSize(int(self.fontSize.text()))
def underline(self):
self.textEdit.setFontUnderline(True)
def disableUnderline(self):
self.textEdit.setFontUnderline(False)
def commander(self):
save(self)
self.textEdit.setHtml('<u>hi</u>')
self.saveButton.clicked.connect(lambda: save(self))
self.fontButton.clicked.connect(lambda: saveFont(self))
self.underlineButton.clicked.connect(lambda: underline(self))
self.disableUnderlineButton.clicked.connect(lambda: disableUnderline(self))
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.saveButton.setText(_translate("MainWindow", "Save text", None))
self.fontButton.setText(_translate("MainWindow", "Save Font", None))
self.menuTest.setTitle(_translate("MainWindow", "test", None))
self.underlineButton.setText(_translate("MainWindow", "Uon", None))
self.disableUnderlineButton.setText(_translate("MainWindow", "Uoff", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I've attempted to fix this with HTML, and other formats but couldn't get it to work.
It's because you're converting the text to plain text with toPlainText, which doesn't contain any formatting information.
yourFile.write(str(self.textEdit.toPlainText()))
If you want to maintain the formatting, you need to use toHtml.
yourFile.write(str(self.textEdit.toHtml()))
Be aware that this isn't the same thing as rtf. It's not even entirely standard HTML and it will likely display a bit differently if you try to look at it in another HTML viewer besides the QTextEdit. In my experience, the HTML generated from the QTextEdit's is pretty ugly, and only really works well if you plan on only displaying it inside QTextEdits.
I'm pretty new to python and especially to pyqt4 but I would like to learn it right.
Therefore I want to build my new learning project in three different files.
One main.py file in which I call everything. One by the QDesigner generated UI file (Main_Ui.py) and one Main_Functions.py file in which I would like to keep all my functions.
My first question would therefore be whether this is a "good programming style" or should I do it in a different way?
During the implementation of my idea I got stuck with calling the function. The console says "TypeError: connect() slot argument should be a callable or a signal, not 'NoneType'".
Could you please help me and tell me what I did wrong as I googled now nearly all day long and I could not find anything that would fit or I didn't understood it. It would be really great and would help me a lot learning how to do it right :)
Thank you very much :)
Here is my examplary code so far:
Main.py
import sys
from PyQt4 import QtGui, QtCore
from Main_Ui import Ui_MainWindow
import Main_Functions as f
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.btn1.clicked.connect(f.download(self.ui))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
Main_Functions.py
def download(window):
window.completed = 0
while window.completed < 100:
window.completed += 0.0001
window.progress.setValue(window.completed)
And the probably not so interesting Main_Ui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Main.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(1000, 848)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayoutWidget = QtGui.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(210, 150, 2, 2))
self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.tableWidget = QtGui.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(50, 120, 256, 192))
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(1)
self.tableWidget.setRowCount(1)
item = QtGui.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
self.progress = QtGui.QProgressBar(self.centralwidget)
self.progress.setGeometry(QtCore.QRect(180, 550, 118, 23))
self.progress.setProperty("value", 24)
self.progress.setObjectName(_fromUtf8("progress"))
self.btn1 = QtGui.QPushButton(self.centralwidget)
self.btn1.setGeometry(QtCore.QRect(490, 200, 75, 23))
self.btn1.setObjectName(_fromUtf8("btn1"))
self.lbl1 = QtGui.QLabel(self.centralwidget)
self.lbl1.setGeometry(QtCore.QRect(400, 200, 46, 13))
self.lbl1.setObjectName(_fromUtf8("lbl1"))
self.txt1 = QtGui.QLineEdit(self.centralwidget)
self.txt1.setGeometry(QtCore.QRect(330, 160, 113, 20))
self.txt1.setObjectName(_fromUtf8("txt1"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1000, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuDatei = QtGui.QMenu(self.menubar)
self.menuDatei.setObjectName(_fromUtf8("menuDatei"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.actionSchlie_en = QtGui.QAction(MainWindow)
self.actionSchlie_en.setObjectName(_fromUtf8("actionSchlie_en"))
self.menuDatei.addAction(self.actionSchlie_en)
self.menubar.addAction(self.menuDatei.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "T", None))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "1", None))
self.btn1.setText(_translate("MainWindow", "PushButton", None))
self.lbl1.setText(_translate("MainWindow", "TextLabel", None))
self.menuDatei.setTitle(_translate("MainWindow", "Datei", None))
self.actionSchlie_en.setText(_translate("MainWindow", "Schließen", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The problem is your connection
self.ui.btn1.clicked.connect(f.download(self.ui))
You need to pass a callback to the connect function. Here, you're actually calling the download function (which returns None) and passing the results to connect.
You need to do this instead
self.ui.btn1.clicked.connect(lambda: f.download(self.ui))
It does often make sense to separate non-gui business logic out into separate modules. This allows them to be re-used in other modules and GUI's. However, in your case, you're performing GUI modification in code outside your GUI class. That's generally not a very good design and it would be better for you to define those functions inside your MainWindow class.
I'm using Selenium with Python. I'm getting a message in widows Python, when the button is clicked:
Not responding in widows Python
I have the following script:
####file:qu
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
#####
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(340, 110, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(324, 200, 111, 23))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.lineEdit = QtGui.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(390, 240, 151, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL(_fromUtf8("clicked()")), self.log)
QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.log2)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "PushButton", None))
self.pushButton_2.setText(_translate("MainWindow", "PushButton_2", None))
def log(self):############################
from qf import functon
n=functon()
n.log1()
def log2(self):
from qf import functon
n=functon()
n.log3()
###file:qm
# -*- coding: utf-8 -*-
from PyQt4 import QtGui
from PyQt4.QtGui import QApplication
from PyQt4 import QtCore, QtGui
import sys
from qu import Ui_MainWindow
class MainWindow(QtGui.QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
file:qf
from selenium import webdriver
from PyQt4 import QtCore, QtGui
import sys
from qu import Ui_MainWindow
class functon ():
def __init__(self, parent=None):
self.parent=parent
def log1(self):
browser =webdriver.Firefox()
browser.get( "http://google.com" )
def log3(self):
text =unicode(self.lineEdit.text())
print text
“Not responding” in Python.
Actually, You main GUI is not frozen at all but only for the time log gets executed and returns control to the main GUI, as you are not implementing any sort of threading mechanism in your application.
So as a solution, you need to thread log method to not block your main GUI, using threading module, the following is a generic way, you need to read more about threading:
1 - import threading in your qu.py file
2 - Define this method in qu.py as well:
def launch_Selenium_Thread(self):
t = threading.Thread(target=self.log)
t.start()
3 - Change the connect method of pushButton to:
QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL(_fromUtf8("clicked()")), self.launch_Selenium_Thread)
4 -Add in qf.py log3 method, txt parameter:
def log3(self, txt):
text =unicode(txt)
print text
5 - Finally fix in qu.py log2 method:
def log2(self):
from qf import functon
n=functon()
txt = self.lineEdit.text()
n.log3(txt)
I have class Ui_MainWindow(object) that creates a window with a progress bar and class OtherClass(object) that contains method in which the local int variable increments in cycle.
How to connect local variable value change to progres bar value change?
mainGUI.py
import sys
from PyQt4.uic.Compiler.qtproxies import QtGui
from PyQt4 import QtGui
from Ui_MainWindow import Ui_MainWindow
def main():
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Ui_MainWindow.py
from PyQt4 import QtCore, QtGui
from MainGui.OtherClass import OtherClass
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def myButtonSlot(self):
objVar=OtherClass()
objVar.method()
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(389, 332)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.myButtonSlot)
self.verticalLayout.addWidget(self.pushButton)
self.progressBar = QtGui.QProgressBar(self.centralwidget)
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName(_fromUtf8("progressBar"))
self.verticalLayout.addWidget(self.progressBar)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 389, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
OtherClass.py
class OtherClass(object):
def method(self):
for i in range(100): # i want to connect variable i to progress bar value
print i
for j in range(100500):
pass
You need to re-organize your code a little bit.
Firstly, you should never edit the code in the UI module generated by pyuic. Instead, import it into your main module and implement all your application logic there.
Secondly, you should create a main-window class in your main module, and do all the setup inside its __init__ method.
One way to solve your problem of connecting the loop variable to the progress bar, is to make OtherClass a subclass of QObject and emit a custom signal:
from PyQt4 import QtCore
class OtherClass(QtCore.QObject):
valueUpdated = QtCore.pyqtSignal(int)
def method(self):
# i want to connect variable i to progress bar value
for i in range(100):
print i
self.valueUpdated.emit(i)
for j in range(100500):
pass
With that in place, you would then move the setup for pushButton and its slot to "mainGUI.py", and re-generate "Ui_MainWindow.py" with pyuic. A slot would then be added to handle the custom valueChanged signal, which would update the progress bar and also process any pending GUI events.
So "mainGUI.py" would end up looking something like this:
import sys
from PyQt4 import QtGui
from Ui_MainWindow import Ui_MainWindow
from OtherClass import OtherClass
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.myButtonSlot)
self.otherclass = OtherClass(self)
self.otherclass.valueUpdated.connect(self.handleValueUpdated)
def myButtonSlot(self):
self.otherclass.method()
def handleValueUpdated(self, value):
self.progressBar.setValue(value)
QtGui.qApp.processEvents()
def main():
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
The following post has a version that increments the progress bar 10% each time you press the button. And a version which uses a timer to increment the progress bar. (I'm just in the process of learning this myself)
In Qt Designer, add a progress bar and a button. Click on 'Edit Signals/Slots', drag a line from the button to somewhere in the window and when the button is 'pressed()' add a slot(or signal??) called 'button_pressed()' (use the + button to make this). When you have done this, the OK button is greyed out - select the slot you made, and press OK.
Save the file as ui_MainWindow.ui (note the capitals carefully).
Convert to a py file using the batch file >
pyuic4 -x ui_MainWindow.ui -o ui_MainWindow.py
This file should look something like....(you don't need to edit this).
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.progressBar = QtGui.QProgressBar(self.centralwidget)
self.progressBar.setGeometry(QtCore.QRect(110, 90, 118, 23))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName(_fromUtf8("progressBar"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(120, 200, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("pressed()")), MainWindow.button_pressed)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "PushButton", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Create a 'program.py' file. This is the file you will run...
import sys
from PyQt4 import QtGui
#from PyQt5 import QtCore, QtGui, QtWidgets #works for pyqt5
from mainWindow import MainWindow
def main():
#app = QtWidgets.QApplication (sys.argv) #works for pyqt5
app = QtGui.QApplication (sys.argv) #works for pyqt4
m = MainWindow ()
m.show ()
sys.exit (app.exec_ () )
if __name__ == '__main__':
main ()
Now this is where the good stuff happens when you subclass the mainwindow. Call this file 'mainWindow.py'. Careful with the capitalizations.
from PyQt4 import QtCore, QtGui
from ui_MainWindow import Ui_MainWindow #note the capitalization
class MainWindow (QtGui.QMainWindow):
def __init__ (self, parent = None):
super (MainWindow, self).__init__ ()
self.ui = Ui_MainWindow ()
self.ui.setupUi (self)
#------------do your custom stuff from here on-----------
self.progress = 0 #Start value of progress bar
self.ui.progressBar.setValue(self.progress)
def button_pressed(self):
print('button pressed')
self.ui.statusbar.showMessage(str(self.progress)) #this is at bottom left of window. Discovered this accidentially when doing this!
self.ui.progressBar.setValue(self.progress)
self.progress+=10
There is a good tutorial here which I used to create an alternate 'mainWindow.py' which uses a timer to increment the progress bar. It does not block the code with a loop using sleep or by doing a CPU intensive loop. I don't understand multithreading, multi-processor options yet to comment on using these.
#from PyQt5 import QtCore, QtGui, QtWidgets #works for PyQt5
from PyQt4 import QtCore, QtGui
from ui_MainWindow import Ui_MainWindow #note the capitalization
class MainWindow (QtGui.QMainWindow):
def __init__ (self, parent = None):
super (MainWindow, self).__init__ ()
self.ui = Ui_MainWindow () #same name as appears in mainWindowUi.py
self.ui.setupUi (self)
self.progress = 0 #Start value of progress bar
self.ui.progressBar.setValue(self.progress)
self.timer = QtCore.QBasicTimer()
def button_pressed(self):
self.timerEvent(64) #this needs an argument to work but I'm not sure what is is yet so I just put in some random number
def timerEvent(self, e):
self.ui.progressBar.setValue(self.progress)
if self.progress >=100:
self.timer.stop()
else:
if self.timer.isActive():
pass
else:
self.timer.start(10,self) #10 milliseconds
self.progress+=1
You have to use a signal and slot...and multiprocessing or multithreading.
There's a good example here that specifically takes you through the progress bar:
ZetCode Progress Bar
Also, question has been answered here before:
SO Progress Bar