@martin I take this opportunity to thank you for your tutorials, very good.
One more time i take your code and modified it .
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(MainWindow, self).__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(MainBase, self).__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_()