Here's the sample code that I want to run:
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("Dummy PyQt file")
window.show()
The file is saved as sample.py. The following command isn't working:
$ python ./sample.py
You need to start the Qt event loop by calling app.exec_() once you have initialised the widgets and called show() on your main window.
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("Dummy PyQt file")
window.show()
app.exec_()
You need to call app.exec_() to start the Qt event loop. Without it the program exits immediately before anything can be shown on screen.
Related
I created a GUI where if you click to a button than it will open a python file. But after the click I want to just see the console. How can I hide the GUI or close without closing the console part?
(Windows 10, Python 3.8.5)
You can hide it.
from PyQt5 import QtWidgets
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win = QtWidgets.QWidget()
win.show()
win.setVisible(False)
sys.exit(app.exec_())
I'm newbie.
I want to click a pushButton to open a new window and take text from main window lineEdit and copy to new pop-up window lineEdit.
So far I an create new window but can't access lineEdit. No errors, app is not responding.
This is what I have:
from PyQt5.QtWidgets import QApplication
from PyQt5 import uic
app = QApplication([]) #Main Window
ui = uic.loadUi(r"D:\UI_test\gui\main_gui_TT.ui")
appedit = QApplication([]) #Pop-up
uiedit = uic.loadUi(r"D:\UI_test\gui\input_TT.ui")
def edit1():
uiedit.show()
appedit.exec_()
uiedit.lineEdit_CC.setText('text') <-this line is a problem
ui.pushButton_1edit.pressed.connect(edit1)
ui.show()
app.exec_()
Please help what is wrong here?
You should only have a single QApplication even if you have many windows, considering the above the solution is:
from PyQt5.QtWidgets import QApplication
from PyQt5 import uic
app = QApplication([]) # Main Window
ui = uic.loadUi(r"D:\UI_test\gui\main_gui_TT.ui")
uiedit = uic.loadUi(r"D:\UI_test\gui\input_TT.ui")
def edit1():
uiedit.show()
uiedit.lineEdit_CC.setText("text")
ui.pushButton_1edit.pressed.connect(edit1)
ui.show()
app.exec_()
Hi recently I started experimenting with the PyQt5 GUI library since Tkinter doesn't look very modern or nice. I am trying to create a very simple window with a button on it, but for some reason it opens a new window for the button. How can I solve this? Here is the code:
from PyQt5 import QtWidgets
app = QtWidgets.QApplication([])
window = QtWidgets.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("UI")
window.show()
btn = QtWidgets.QPushButton("Test")
btn.resize(100,100)
btn.move(100,100)
btn.show()
(app.exec_())
I am aware that I should be using classes for the GUI, but I'd like to learn the basics before I start doing that.
For a widget like the QPushButton to be part of a window, the widget must be fulfilled:
be child of the window or
be a child of some child from the window or
it is part of a layout that belongs to the window.
In your case QPushButton does not meet any of it so it will be a new window.So it can be solved using the 1 or 3 rule:
import sys
from PyQt5 import QtWidgets
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("UI")
btn = QtWidgets.QPushButton("Test", window)
btn.resize(100,100)
btn.move(100,100)
window.show()
sys.exit(app.exec_())
Or:
import sys
from PyQt5 import QtWidgets
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("UI")
btn = QtWidgets.QPushButton("Test")
lay = QtWidgets.QHBoxLayout(window)
lay.addWidget(btn)
window.show()
sys.exit(app.exec_())
So, I am trying to run a very simple program (a window) in Pycharm that is running anaconda 2.7 & PyQt4. Whenever I click the Run button it opens my program but closes the window too fast for me to even see it. May anyone, please help? Thank you!
P.S.
I'm very new to programming.
{__author__ = 'Jay'
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.show()}
You need to block the execution of the program after you call window.show() so that the window object remains active otherwise it will be garbage collected. app.exec_() does this for you.
{__author__ = 'Jay'
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.show()
app.exec_()} # added this line
A Canopy user here learning about PySide. When I run the demo code below, QApplication complains the event loop is already running.'
import sys
from PySide.QtCore import *
from PySide.QtGui import *
# Create a Qt application
#app = QApplication(sys.argv) #QApplication complains an instance already exists
app = QApplication.instance() #So we just ask for the instance.
#app.aboutToQuit.connect(app.deleteLater)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
sys.exit()
So how can I get this simple code to run?
Yes, Pylab is a mode of IPython which starts an event loop for the IPython front end so that you can interact at the IPython command line with your GUI.
Here's an simple example of code which will run with or without Pylab.
import sys
from PySide import QtGui
app = QtGui.QApplication.instance()
standalone = app is None
if standalone:
app = QtGui.QApplication(sys.argv)
wid = QtGui.QWidget()
wid.resize(250,150)
wid.setWindowTitle('Simple')
wid.show()
if standalone:
sys.exit(app.exec_())
else:
print "We're back with the Qt window still active"