DosSantos | 2021-04-07 13:08:58 UTC | #1
@martin I take this opportunity to thank you for your tutorials, very good. One more time i take your code and modified it :blush:. For testing news scripts that is good, but i only have one problem. When I assign the code to a new button I need to restart my IDE. I use pycharm. Is it possible to solve this without other ways ? Thanks
from PyQt5.QtWidgets import QMainWindow, QApplication, QAction
from PyQt5.QtCore import Qt, QThread, QCoreApplication
from PyQt5 import uic
""" *** WARNING """
""" First need create dialog with tree buttons (bt_one, bt_two, bt_tree) with designer and save file window.ui """
import os # Used in Testing Script
os.system("pyuic5.exe resources\\window.ui -o resources\\window.py") # Used in Testing Script
from resources.window import Ui_MainWindow
import sys
qtCreatorFile = "resources\\window.ui" # Type your file path
""" This block used for testing script """
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.reload = None
self.setupUi(self)
self.mainbase = self
self.load_ui()
def load_ui(self):
print("loading ui")
cls, baseclass = uic.loadUiType(qtCreatorFile)
self.ui = Window()
self.ui.setupUi(self.ui)
""" Move the central widget from the loaded UI main window our visible one."""
self.setCentralWidget(self.ui.centralWidget())
if not self.reload:
menu = self.menuBar()
ui = menu.addMenu("UI")
self.reload = QAction("Reload UI")
self.reloadexe = QAction("Reload App")
self.quit = QAction("Quit")
self.reload.triggered.connect(self.load_ui)
self.reloadexe.triggered.connect(self.load_app)
self.quit.triggered.connect(self.close)
self.reload.setShortcut(Qt.CTRL | Qt.Key_A) # You can reload with >Ctrl-A
self.reloadexe.setShortcut(Qt.CTRL | Qt.Key_Q) # You can run test >app >with Ctrl-Q
self.quit.setShortcut(Qt.Key_Escape) # You can terminate with Ctrl-R
ui.addActions([self.reload, self.reloadexe, self.quit])
@staticmethod
def load_app():
import os, time
os.system("pyuic5.exe resources\\window.ui -o resources\\window.py")
runner()
""" End block used for testing script """
class MainBase(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
cls, baseclass = uic.loadUiType("resources\\window.ui")
class Window(cls, baseclass):
pass
self.ui = Window()
self.ui.setupUi(self.ui)
self.ui.show()
""" First need create tree buttons (bt_one, bt_two, bt_tree) with designer >and save file window.ui """
self.ui.bt_one.clicked.connect(lambda: self.ui.lineedit.setText("button >one"))
self.ui.bt_two.clicked.connect(lambda: self.displaytxt("button two"))
self.ui.bt_tree.clicked.connect(lambda: self.displaytxt("button tree"))
""" first write "self." and connection, after this rename to self.ui. ... """
""" second for test your new button need restart your IDE """
""" put more connections and all other code bellow """
def displaytxt(self, x):
self.ui.lineedit.setText(x)
""" Used in Testing Script """
def runner():
from resources.window import Ui_MainWindow
apptest = QCoreApplication
MainBase()
apptest.exec_()
app = QApplication([])
w = MainWindow()
w.show()
app.exec_()
martin | 2021-04-07 13:12:26 UTC | #2
Hi @DosSantos I missed this when you originally posted it -- I've fixed up the code now so it's clearer. Do you still have this problem/have you found a solution?
You're not the only person who has reported it, but I've not been able to reproduce it myself. That said, normally what I do when developing with Qt is to open a separate terminal (command prompt) and run the code from there. I started doing that when I was using an IDE which would die if the Qt app hung -- it's now just a habit!
Since you're usually just running the same command over and over, it's just a case of hitting the ^ arrow on the keyboard (recall last command) and pressing Enter to repeat.
Would be great to hear of other solutions though.
Never miss an update
Enjoyed this? Subscribe to get new updates straight in your Inbox.