PyQt “Not responding” - python

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)

Related

How to display custom python QTextEdit effects like underline in txt or rtf format?

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.

Dynamically add and sort items in tableview python

Reading data in from a simple dict d={key:['desc',dateadded]}
Goal is to add data into table view and sort by date in the list. Cannot get any data to add when trying to put table on main window.
The issue lies with how I am placing the table on the main window. I have a working widget with table and am now trying to combine the two files. Currently the application crashes. Dont want you to write the code but a nudge in the right direction would be a big help.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'display.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4.QtGui import (QMainWindow, QApplication)
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
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 __init__(self):
#app = QApplication(sys.argv)
self.window = QMainWindow()
self.setupUi(self.window)
self.window.show()
self.setmydata()
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(296, 478)
MainWindow.setIconSize(QtCore.QSize(0, 0))
MainWindow.setAnimated(False)
MainWindow.setTabShape(QtGui.QTabWidget.Rounded)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.tableView = QtGui.QTableView(self.centralwidget)
self.tableView.setGeometry(QtCore.QRect(20, 50, 261, 391))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tableView.sizePolicy().hasHeightForWidth())
self.tableView.setSizePolicy(sizePolicy)
self.tableView.setEditTriggers(QtGui.QAbstractItemView.AllEditTriggers)
self.tableView.setDragEnabled(True)
self.tableView.setAlternatingRowColors(True)
self.tableView.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
self.tableView.setSortingEnabled(False)
self.tableView.setObjectName(_fromUtf8("tableView"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(20, 10, 121, 31))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(160, 10, 121, 31))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 296, 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 setmydata(self):
d={"12345678":["do some stuff here that needs to be in a large cell to wrap text","1"],"12343378":["do stuff","1"]}
self.data = {}
keys=d.keys()
self.data['col1']=keys
self.data['col2']=[]
for key in keys:
self.data['col2'].append(d[key][0])
horHeaders = []
for n, key in enumerate(sorted(self.data.keys())):
horHeaders.append(key)
for m, item in enumerate(self.data[key]):
newitem = QTableWidgetItem(item)
self.setItem(m, n, newitem)
self.setHorizontalHeaderLabels(horHeaders)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "Add New", None))
self.pushButton_2.setText(_translate("MainWindow", "Save", None))
app = QApplication(sys.argv)
frame = Ui_MainWindow()
#frame.show()
app.exec_()
After digging through the code I found line
self.tableView = QtGui.QTableView(self.centralwidget)
needed to be QTableWidget rather than a View. This allows for me to set values and so on.

How do I get variables from my main module in Python?

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.

How to refresh items in Qcombobox [python]?

I have a list of data which is shown in the Qcombobox (using 'addItems')
Now - if the list is changed by loading data from a file with pushbutton - i don't see the new data in the combobx.
The new data is there (I can print it after loading)
What do I miss?
below is the code for the simplified gui
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QFileDialog
import pandas as pd
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 __init__(self):
self.comboData=['None']
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(456, 172)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.comboBox = QtGui.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(120, 60, 69, 22))
self.comboBox.setObjectName(_fromUtf8("comboBox"))
self.comboBox.setEditable(True)
self.comboBox.addItems(self.comboData)
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 60, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.load)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 456, 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(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "Load", None))
def load(self):
fileName=QFileDialog.getOpenFileName(MainWindow,'Load File')
data_pd=pd.read_csv(fileName,index_col=0,na_filter=False)
self.comboData=list(data_pd.var1)
print(self.comboData)
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_())
you only load the data to self.comboData but do not load them to self.comboBox.
At the end of
def load(self):
add the following lines:
self.comboBox.clear() # delete all items from comboBox
self.comboBox.addItems(self.comboData) # add the actual content of self.comboData
As the model used by QComboBox emits the dataChanged() signal whenever the data in an item are changed, the combobox is repainted automatically and it is not necessary to update the combobox by
self.comboBox.update()

The Code does not work successfully when the function is called

I want to call method (close_ok()) in (main.py ) from method :close_call() in (module_b.py), but the Code does not work successfully when the function is called.
can anyone help me with this problem?
Here's the code for that :
###main.py
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
import sys
from GUI 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)
from module_c import class_c
global b
b=class_c()
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), b.close_call )
def close_ok(self):
##But it can not be done successfully.
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
global myapp
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())
### module_b.py
class class_c (object):
def __init__(self, parent=None):
self.parent=parent
### I want call method (close_ok()) in (menu class) from here
def close_call (self):
from main import MainWindow
t=MainWindow()
t.close_ok()
###GUIpy
# -*- coding: utf-8 -*-
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):
super(Ui_MainWindow, self).__init__()
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"))
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)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "PushButton", None))
you use diffrent instances of the class MainWindow() so it wont close the original window, change it like this:
class MainWindow(QtGui.QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
from module_c import class_c
global b
b=class_c(self) #NOTICE THE CHANGE HERE
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), b.close_call )
def close_ok(self):
self.close()
and change module b to do:
class class_c (object):
def __init__(self, parent=None):
self.parent=parent
def close_call (self):
from main import MainWindow
self.parent.close_ok()# NOTICE THIS CHANGE, parent is now the original MainWindow
this way you pass your original MainWindow instancce to class_c and use it, instead of creating a new instance

Categories