(PyQt) QVBoxLayout shrink upon multiple loading of addWidget - python

Why is the layout shrinking like this and other times going back to normal?
I've created several separate UI files in QtDesigner, one is the MainWindow and the other is a widget for Loading Data.
In order to work with these files, I've created separate child classes of each UI file. In order to add a new widget to the MainWindow I've created a addWidget() function; it works by adding a particular widget to the scrollarea layout. You can see this function in MainWindow.py
Here is the code for __main__.py
import multiprocessing as mp
import os.path
import sys
import time
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import *
from point_spectra_gui.future_.functions import *
from point_spectra_gui.future_.util import delete
from point_spectra_gui.future_.util.excepthook import my_exception_hook
def new():
p = mp.Process(target=main, args=())
p.start()
def connectWidgets(ui):
ui.actionLoad_Data.triggered.connect(lambda: ui.addWidget(LoadData.Ui_Form))
def main():
sys._excepthook = sys.excepthook
sys.excepthook = my_exception_hook
app = QtWidgets.QApplication(sys.argv)
mainWindow = QtWidgets.QMainWindow()
ui = MainWindow.Ui_MainWindow()
ui.setupUi(mainWindow)
connectWidgets(ui)
mainWindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Here is the code for MainWindow.py
from PyQt5 import QtWidgets
from point_spectra_gui.future_.functions import *
from point_spectra_gui.future_.util import *
from point_spectra_gui.ui import MainWindow
class Ui_MainWindow(MainWindow.Ui_MainWindow):
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
super().setupUi(MainWindow) # Run the basic window UI
self.menu_item_shortcuts() # set up the shortcuts
def addWidget(self, object):
widget = object()
widget.setupUi(self.scrollArea)
self.widgetLayout = QtWidgets.QVBoxLayout()
self.widgetLayout.setObjectName("widgetLayout")
self.verticalLayout_3.addLayout(self.widgetLayout)
self.widgetLayout.addWidget(widget.get_widget())
def menu_item_shortcuts(self):
self.actionExit.setShortcut("ctrl+Q")
self.actionCreate_New_Workflow.setShortcut("ctrl+N")
self.actionOpen_Workflow.setShortcut("ctrl+O")
self.actionRestore_Workflow.setShortcut("ctrl+R")
self.actionSave_Current_Workflow.setShortcut("ctrl+S")
Here is the code of the child class LoadData.py
from PyQt5 import QtWidgets
from point_spectra_gui.ui.LoadData import Ui_loadData
class Ui_Form(Ui_loadData):
def setupUi(self, Form):
super().setupUi(Form)
self.connectWidgets()
def get_widget(self):
return self.groupBox
def connectWidgets(self):
self.newFilePushButton.clicked.connect(lambda: self.on_getDataButton_clicked())
# self.get_data_line_edit.textChanged.connect(lambda: self.get_data_params())
# self.dataname.textChanged.connect(lambda: self.get_data_params())
def on_getDataButton_clicked(self):
filename, _filter = QtWidgets.QFileDialog.getOpenFileName(None, "Open Data File", '.', "(*.csv)")
self.fileNameLineEdit.setText(filename)
if self.fileNameLineEdit.text() == "":
self.fileNameLineEdit.setText("*.csv")
**Edit
Upon trying this again and then shrinking the window. The layout goes back to normal.
This to me tells me it's not a problem with my code, it's the way the Qt handles the adding of widgets. I still do not understand why this is happening though. So any insight into how this is happening is very much appreciated.

This problem is with the Form.resize() inside the generated code.
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
To fix this you'll need to go into QtDesigner and set the geometry back to it's default the Layout size by clicking the red circled item.
This, in essence, deletes the resize method call
You can then convert again with pyuic

Related

Blank Third window opening up with Second Window PyQt

Ok I'm having an issue where when I click a button on my first window to open up the second window, a third blank window is opening up with it. I've been trying to figure out where in the code this is initializing the 3rd. hoping someone can help.
Here is the code for the first main window...
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
from PyQt5 import uic
from PyQt5 import QtCore
import sys
class GuitarSpec_ViewWindow(QMainWindow):
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
def __init__(self):
super(GuitarSpec_ViewWindow, self).__init__()
uic.loadUi("guitarSpecViewLayout.ui", self)
self.click = self.findChild(QPushButton, "click_pushButton")
self.click.clicked.connect(self.openEditWindow)
self.show()
def openEditWindow(self):
from guitarSpecEditorTestDelete import Ui_GuitarSpec_EditorWindow
self.window = QMainWindow()
self.Ui = Ui_GuitarSpec_EditorWindow()
self.Ui.setupUi(self.window)
self.window.show()
app = QtWidgets.QApplication(sys.argv)
UIWindow = GuitarSpec_ViewWindow()
app.exec_()
sys.exit(app.exec_())
The Second window when it opens automatically opens up a blank third window. here is that code...
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5 import uic
class Ui_GuitarSpec_EditorWindow(QMainWindow):
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
def setupUi(self, GuitarSpec_EditorWindow):
GuitarSpec_EditorWindow.setObjectName("GuitarSpec_EditorWindow")
uic.loadUi("guitarSpecEdit.ui", self)
self.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
GuitarSpec_EditorWindow = QtWidgets.QMainWindow()
ui = Ui_GuitarSpec_EditorWindow()
ui.setupUi(GuitarSpec_EditorWindow)
GuitarSpec_EditorWindow.show()
sys.exit(app.exec_())
Any Help would be greatly appreciated! I am new to this so it's probably a fairly simple solution.

How to save a gui created with qt and python as an image

I am trying to use qt creator and python to generate flowcharts with values. I want to save the generated flowchart as an image after, but I cannot figure out how to do it. Here is my attempt:
import sys
from PyQt5 import QtWidgets, uic
from PyQt5.QtGui import *
from mainwindow import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
screen = app.primaryScreen()
screenshot = screen.grabWindow(window)
screen.save('screenshot.png', 'png')
The fact that the show method is called does not imply that it is shown instantly, but rather that this method notifies the OS so that the window is created. A possible solution is to use a QTimer, on the other hand the grabWindow method grabs a window using the associated WID, in this case it is better to use the grab method:
from functools import partial
def take_screenshot(widget):
pixmap = widget.grab()
if not pixmap.isNull():
pixmap.save("/fullpath/of/screenshot.png", "PNG")
QApplication.quit()
def main():
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
QTimer.singleShot(500, partial(take_screenshot, window))
app.exec_()
if __name__ == "__main__":
main()

Use separate interface file with PyQt

I have an interface file generated from QtDesigner that I want to keep the same in case of changes.
Have a main file called application.py handle all the functions, and one file strictly for interface stuff.
I am using PyQt5.
I have not been able to find any tutorials on this specific question, any pointers would be helpful.
Code from YatsiInterface.py (shorten)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_YatsiWindow(object):
def setupUi(self, YatsiWindow):
YatsiWindow.setObjectName("YatsiWindow")
YatsiWindow.resize(800, 516)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("Terraria.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
YatsiWindow.setWindowIcon(icon)
self.windowLayout = QtWidgets.QWidget(YatsiWindow)
self.windowLayout.setObjectName("windowLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.windowLayout)
self.quitButton.setObjectName("quitButton")
self.buttonsLeftLayout.addWidget(self.quitButton)
YatsiWindow.setCentralWidget(self.windowLayout)
self.statusbar = QtWidgets.QStatusBar(YatsiWindow)
self.statusbar.setObjectName("statusbar")
YatsiWindow.setStatusBar(self.statusbar)
self.retranslateUi(YatsiWindow)
QtCore.QMetaObject.connectSlotsByName(YatsiWindow)
def retranslateUi(self, YatsiWindow):
_translate = QtCore.QCoreApplication.translate
YatsiWindow.setWindowTitle(_translate("YatsiWindow", "Yatsi - Server Interface"))
self.quitButton.setText(_translate("YatsiWindow", "Quit"))
How can I use self.quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit()) with the above code? I know to import the file with from YatsiInterface import Ui_YatsiWindow but I'm in the dark as how to create button functions without editing the interface file.
Edit:
I'll add my broken code below.
import sys
from YatsiInterface import Ui_YatsiWindow
from PyQt5 import QtCore, QtGui, QtWidgets
app = QtWidgets.QApplication([])
YatsiWindow = QtWidgets.QMainWindow()
ui = Ui_YatsiWindow()
ui.setupUi(YatsiWindow)
# Here's the bad part
ui.setupUi.btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
# Up there ^
YatsiWindow.show()
sys.exit(app.exec_())
Thanks for your help.
This would be the way to connect the signal
ui.quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)
That being said, you probably don't want to do this. You should probably connect to the .close method of the window. When the window closes, by default, Qt will exit the event loop and quit.
ui.quitButton.clicked.connect(YatsiWindow.close)
from YatsiInterface import Ui_YatsiWindow
from PyQt5 import QtCore, QtGui
class my_application(QtGui.QWidget, Ui_YatsiWindow):
def __init__(self):
super(my_application, self).__init__()
self.setupUi(self)
self.quitButton.clicked.connect(self.my_mythod)
def my_method(self):
pass #all your code for the buttons clicked signal
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ui = my_application()
ui.show()
sys.exit(app.exec_())
This my_application class is inheriting your user interface class. Now, you can write your button related functions without editing the user interface file.
self.quitButton.clicked.connect(self.close)
this will close your user interface when button is clicked.

Ui_MainWindow has no attribute 'show'

I'm writing a program which will have multiple windows. I have a main program (attached) that calls the Ui files (that have been converted into .py). The main window and customise window open correctly (the first two listed) but neither the third or fourth windows open correctly, giving me the error
'Ui_MainWindow' object has no attribute 'show'
The main program;
from PyQt4 import QtCore, QtGui
import sys
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainwin = main_menu_ui.Ui_MainWindow()
mainwin.show()
sys.exit(app.exec_())
def openCustomise(self):
customiseOpen = question_set_menu_ui.Ui_MainWindow()
customiseOpen.show()
sys.exit(app.exec_())
def openQuiz(self):
quizOpen = quiz_window_ui.Ui_MainWindow()
quizOpen.show()
sys.exit(app.exec_())
def addNewSet(self):
addNewOpen = question_set_edit_ui.Ui_MainWindow()
addNewOpen.show()
sys.exit(app.exec_())
Sorry if I'm missing something obvious, I'm learning Qt/Python for college.
The auto-generated UI class that you are importing extends object and doesn't have a show method (open up the .py file for yourself and verify this).
In general, you should structure your GUIs like this:
from PyQt4 import QtCore, QtGui
import sys
from layout_file import main_menu_ui
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = main_menu_ui()
self.ui.setupUi(self)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainwin = MyForm()
mainwin.show()
sys.exit(app.exec_())
You import your UI from your autogenerated UI file. You have a class that contains your GUI logic. It then sets up your UI layout from your imported UI in its __init__() method.

how to call a python script on button click using PyQt

I have created a form using PyQt4 which has a push button. On this push button I want to call another python script which looks like this:
File1.py:
import sys
from PyQt4 import QtCore, QtGui
from file1_ui import Ui_Form
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
File1_ui.py
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(120, 200, 95, 20))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Form.close)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("Form", "Close", None, QtGui.QApplication.UnicodeUTF8))
File2.py
import sys
from PyQt4 import Qt
from taurus.qt.qtgui.application import TaurusApplication
app = TaurusApplication(sys.argv)
panel = Qt.QWidget()
layout = Qt.QHBoxLayout()
panel.setLayout(layout)
from taurus.qt.qtgui.panel import TaurusForm
panel = TaurusForm()
model = [ 'test/i1/1/%s' % p for p in props ]
panel.setModel(model)
panel.show()
sys.exit(app.exec_())
File1_ui.py is created from the Qtdesigner and then I am using File1.py to execute it.So File2.py when executed alone opens up a panel and displays few attributes.I want this script to be called on the button click in the first form(file1.py) which I created using Qtdesigner.Could you let me know how I could achieve this functionality.Thanks.
You will need to make some modifications to File2.py to make the appropriate calls depending on whether it is running standalone or not. When you are launching the script via File1.py there will already be a QApplication instance with event loop running, so trying to create another and run its event loop will cause problems.
Firstly, move the core part of your script into its own function. This will allow you to easily call it from File1.py. You can then handle the case where the script is running standalone and needs to create a QApplication instance and start its event loop. (I am not familiar the the taurus library you are using, but you can probably substitute TaurusApplication for QtGui.QApplication)
File2.py:
import sys
from PyQt4 import QtCore, QtGui
def runscript():
panel = QtGui.QWidget()
layout = QtGui.QHBoxLayout(panel)
return panel # Must return reference or panel will be deleted upon return
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
panel = runscript()
panel.show()
sys.exit(app.exec_())
Assuming your files are in the same directory you can simply write import File2 and use File2.runscript() to run your code. You then just need to connect the function to your pushbuttons clicked() signal to run it. The only problem here is that the reference to the QWidget returned from the runscript() function will be lost (and the object deleted) if you connect directly to runscript(). For this reason I created a method launch_script() which saves a reference in MyForm.
File1.py:
import sys
from PyQt4 import QtCore, QtGui
from file1_ui import Ui_Form
import File2
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
# This is a bit of a hack.
self.ui.pushButton.clicked.connect(self.launch_script)
def launch_script(self):
self.panel = File2.runscript()
self.panel.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
I don't use Qt Designer, so I don't know the correct way to go about connecting the signal to launch_script(). The code I have written should work, but obviously violates OOP principles and is dependent on the name of the pushbutton widget assigned by the software.

Categories