I have written a python 3.4 code which uses pyqt4 GUI modules but when i run the module it does not show anything kindly help
import sys
from PyQt4 import QtGui,QtCore
class window(QtGui.QMainWindow):
def _init_(self):
super(Window, self)._init_()
self.setGeometry(50,50,500,300)
self.setWindowTitle("Tallman Server")
self.setWindowIcon(QtGui.QIcon("tracking.png"))
self.home()
def home():
btn=QtGui.QPushButton("Quit",self)
btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
self.show()
def run():
app=QtGui.QApplication(sys.argv)
GUI=window()
sys.exit(app.exec_())
run()
Firstly, the function's name is __init__ instead of _init_.
Secondly, you have to add the self parameter to home().
Those changes will solve your problem.
Modified code:
import sys
from PyQt4 import QtGui,QtCore
class window(QtGui.QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("Tallman Server")
self.setWindowIcon(QtGui.QIcon("tracking.png"))
self.home()
def home(self):
btn=QtGui.QPushButton("Quit",self)
btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
self.show()
def run():
app=QtGui.QApplication(sys.argv)
GUI=window()
sys.exit(app.exec_())
run()
Related
In pyqt5 when i try to close a tread for a my program Ui freezes and stop responding.I'm new to pyqt5 couldn't find a answer for this.
These are the methods in main window(GUI) file
def clickButton_on(self):
print("clicked on/off")
ControlPanel.start()
def clickButton_Off(self):
print("clicked on/off")
ControlPanel.stop()
This is the controlPanel class methods
#classmethod
def start(cls):
print('start method fired')
cls.th = threading.Thread(target=cls.test,args=(cls.attr,))
cls.th.start()
#classmethod
def stop(cls):
print('stop method fired')
cls.th.join()
Full code to recreate the issue
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.uic import loadUi
import sys
import threading
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow,self).__init__()
self.b1 = QtWidgets.QPushButton(self)
self.b1.setText("on")
self.b2 = QtWidgets.QPushButton(self)
self.b2.setText("off")
self.b2.move(50,50)
self.b1.clicked.connect(self.clickButton_on)
self.b2.clicked.connect(self.clickButton_Off)
def clickButton_on(self):
print("clicked on/off")
ControlPanel.start()
def clickButton_Off(self):
print("clicked on/off")
ControlPanel.stop()
class ControlPanel:
n=2
#classmethod
def start(cls):
print('start method fired')
cls.th = threading.Thread(target=cls.test,args=(cls.n,))
cls.th.start()
#classmethod
def stop(cls):
print('stop method fired')
cls.th.join()
#classmethod
def test(cls,n):
print('test method fired')
n= 0
while True:
n += 0.1
print(f'thread running n = {n}')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
windows completely freezes. It doesnt close the thread with .join() it thread keeps running.
i make test.py using pyqt, run python test.py, it run properly but after i make .Elf file it shows error Import-error: No module named 'PyQt4'
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
qbtn = QtGui.QPushButton('Quit', self)
qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
qbtn.resize(qbtn.sizeHint())
qbtn.move(50, 50)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Quit button')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
i want to make elf file of this test.py
First of all, similar questions have been answered before, yet I need some help with this one.
I have a window which contains one button (Class First) and I want on pressed, a second blank window to be appeared (Class Second).
I fiddled with the code copied from this question: PyQT on click open new window, and I wrote this code:
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
import design1, design2
class Second(QtGui.QMainWindow, design2.Ui_MainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.setupUi(self)
class First(QtGui.QMainWindow, design1.Ui_MainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialog = Second(self)
def on_pushButton_clicked(self):
self.dialog.exec_()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
but on_pressed, this error message appears:
AttributeError: 'Second' object has no attribute 'exec_'
(design1 and design2 have been derived from the Qt designer.)
Any thought would be appreciated.
Here I'm using the show method.
Here is a working example (derived from yours):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
class Second(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.pushButton = QtGui.QPushButton("click me")
self.setCentralWidget(self.pushButton)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialog = Second(self)
def on_pushButton_clicked(self):
self.dialog.show()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
If you need a new window every time you click the button, you can change the code that the dialog is created inside the on_pushButton_clicked method, like so:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
class Second(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.pushButton = QtGui.QPushButton("click me")
self.setCentralWidget(self.pushButton)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialogs = list()
def on_pushButton_clicked(self):
dialog = Second(self)
self.dialogs.append(dialog)
dialog.show()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Can't work out how to embed a window in a main window using classes:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Qt4 tutorial using classes
This example will be built
on over time.
"""
import sys
from PyQt4 import QtGui, QtCore
class Form(QtGui.QWidget):
def __init__(self, MainWindow):
super(Form, self).__init__()
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setGeometry(50, 50, 1600, 900)
new_window = Form(self)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
main_window = MainWindow()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
This is supposed to be the single most basic bit of code using classes. How do I get the second window to show please.
As ekhumoro already pointed out, your widget needs to be a child of your mainWindow. However, I do not think that you need to call show for the widget, since it anyways gets called as soon as its parent (MainWindow) calls show. As mata pointed out correctly, the proper way to add a Widget to a MainWindow instance is to use setCentralWidget. Here is a working example for clarification:
import sys
from PyQt4 import QtGui, QtCore
class Form(QtGui.QWidget):
def __init__(self, parent):
super(Form, self).__init__(parent)
self.lbl = QtGui.QLabel("Test", self)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setGeometry(50, 50, 1600, 900)
new_window = Form(self)
self.setCentralWidget(new_window)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
main_window = MainWindow()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
I am developing a PyQT program using the Spyder IDE (which uses IPython) on a Windows 7 machine, and I am having trouble with the statement sys.exit(app.exec_()). I have read this post
What the error when I close the dialog
and tried using just app.exec_(). When I use just app.exec_(), however, the GUI very briefly opens and then closes immediately. Here is my minimum (not) working example:
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
btn = QtGui.QPushButton('Button', self)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
app.exec_()
#sys.exit(app.exec_())
if __name__ == '__main__':
main()
This is my first post on stackoverflow, so if I could improve this post in any way, please let me know.
I figured out the solution. Apparently you do not need to include app.exec_() at all, according to
http://cyrille.rossant.net/making-pyqt4-pyside-and-ipython-work-together/
The following code works in Spyder:
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
btn = QtGui.QPushButton('Button', self)
self.show()
ex= Example()