My question will be used in two .py:
main.py:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from tag_pivot import *
from mainWindow import Ui_MainWindow
import sys
a = 'tag_'
b = 'pivot_'
c = 'STP_'
d = '_'
e = 'MTD_Level'
f = '()'
g = a+b+c+d+e+f
print(g)
class MainApp(QMainWindow, Ui_MainWindow):
def slot_method(self):
combo = self.comboBox.currentText()//combo is local variable as it is from self the GUI
combo2 = self.comboBox_2.currentText()
eval(a + b + combo + d + combo2 + f)
def __init__(self):
super(MainApp, self).__init__()
self.setupUi(self)
self.init_connections()
def init_connections(self):
self.pushButton.clicked.connect(self.slot_method)
def main():
app = QApplication(sys.argv)
ex = MainApp()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
mainWindow.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(100, 160, 111, 51))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(410, 152, 151, 51))
self.pushButton.setObjectName("pushButton")
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(88, 281, 151, 71))
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.comboBox.setItemText(0, _translate("MainWindow", "STP_"))
self.comboBox.setItemText(1, _translate("MainWindow", "MM_"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.comboBox_2.setItemText(0, _translate("MainWindow", "Intra_Level"))
self.comboBox_2.setItemText(1, _translate("MainWindow", "MTD_Level"))
self.comboBox_2.setItemText(2, _translate("MainWindow", "YTD_Level"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
my question is can I make the combo (come from self.comboBox.currentText()) from local variable to global variable as I want to assign the combo which is a string to another functions outside the class MainAPP. I am not sure how to make it happen, I assume others may have the same problem, so I post it here.
if I try to use ex.combo in main():
def main():
app = QApplication(sys.argv)
ex = MainApp()
gg = ex.combo
ex.show()
sys.exit(app.exec_())
the sys told me:
Traceback (most recent call last):
File "C:/Users/Michael Su/Desktop/Michael_python/Tag_pivot/main.py", line 86, in <module>
main()
File "C:/Users/Michael Su/Desktop/Michael_python/Tag_pivot/main.py", line 80, in main
gg = ex.combo
AttributeError: 'MainApp' object has no attribute 'combo'
Process finished with exit code 1
Although I'm not sure why the tag_pivot part of the functions is necessary, if all you are trying to do is print specific values, why not print them directly from main instead of the long eval statement and different functions?
print(f"{c}{e}") # prints STP_MTD
Or if you prefer a function, specifically for printing:
def printer_function(*args):
print("".join(args))
This function can then be called from main and given any variables you want printed together. e.g.
printer_function(c, e) # prints STP_MTD
I hope this helps, but if printing is not the only function you had in mind, let me know.
Related
hi i wrote a python program in qt i want to call a function in pushbutton but its not work
my code is:
i write this code in pycharm and run this code but its not work
i tested it in idle , vs , other environment but this is not work. when i use jcopy function to pushbutton event its work and its copy lineEdit text to lineEdit_2 text but when i want to use the jtrans function for translate the text of lineEdit and and copy result to lineEdit2 program is not work. in pycharm and vs , idle dont sent any error but not work,so i write the same consol program that run in terminal that run currectly
from PyQt5 import QtCore, QtGui, QtWidgets
from googletrans import Translator
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(598, 456)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 30, 201, 16))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(150, 220, 131, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 220, 201, 16))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(480, 220, 75, 23))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(30, 60, 551, 131))
self.lineEdit.setText("")
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(30, 250, 541, 141))
self.lineEdit_2.setObjectName("lineEdit_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 598, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.jtrans)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Input Text:"))
self.comboBox.setItemText(0, _translate("MainWindow", "persian"))
self.comboBox.setItemText(1, _translate("MainWindow", "english"))
self.comboBox.setItemText(2, _translate("MainWindow", "arabic"))
self.comboBox.setItemText(3, _translate("MainWindow", "danish"))
self.comboBox.setItemText(4, _translate("MainWindow", "dutch"))
self.label_2.setText(_translate("MainWindow", "output Text:"))
self.pushButton.setText(_translate("MainWindow", "translate"))
# def jcopy(self):
# t1=self.lineEdit.text()
# self.lineEdit_2.setText(t1)
def jtrans(self, translator=None):
srcString = self.lineEdit.text()
srcLang = translator.detect(srcString)
dstLang = self.comboBox.currentText()
translated = translator.translate(srcString, src=srcLang, dest=dstLang)
self.lineEdit_2.setText(translated.text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
jcopy function only is a fuction that i know the pushbutton run the code
the problem is in sippet code:
def jtrans(self, translator=None):
srcString = self.lineEdit.text()
srcLang = translator.detect(srcString)
dstLang = self.comboBox.currentText()
translated = translator.translate(srcString, src=srcLang, dest=dstLang)
self.lineEdit_2.setText(translated.text)
anybody can solve this problem
tnx
You have a trivial error: Why do you set object translator to def jtrans(self, translator=None):? Do you think object translator was magically created? Well, no. If the error message is checked:
Traceback (most recent call last):
File "foo.py", line 74, in jtrans
srcLang = translator.detect(srcString)
AttributeError: 'bool' object has no attribute 'detect'
It is understandable that it is not magically created, but X takes the value of what the default clicked signal sends: a boolean.
Before proposing the PyQt solution, I recommend not modifying the code generated by Qt Designer, so you will have to use pyuic and regenerate the file, I will assume that the generated file is called gui.py.
Keep in mind that the translation process is very time consuming so it could crash the GUI so you will have to run it in another thread. Another problem in your code is that the detect method does not return the language but an object of the Detected class that has the language information.
Considering the above, the solution is:
main.py
import sys
import threading
from PyQt5 import QtCore, QtGui, QtWidgets
from googletrans import Translator
from gui import Ui_MainWindow
class WorkerTranslator(QtCore.QObject):
translateSignal = QtCore.pyqtSignal(str)
def translate(self, text, dst_lang):
threading.Thread(
target=self._translate, args=(text, dst_lang), daemon=True
).start()
def _translate(self, text, dst_lang):
translator = Translator()
src_lang = translator.detect(text).lang
translated = translator.translate(text, src=src_lang, dest=dst_lang)
self.translateSignal.emit(translated.text)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.translate)
self.translator = WorkerTranslator()
self.translator.translateSignal.connect(self.lineEdit_2.setText)
#QtCore.pyqtSlot()
def translate(self):
text = self.lineEdit.text()
dst_lang = self.comboBox.currentText()
self.translator.translate(text, dst_lang)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
├── gui.py
└── main.py
This is a sample code with just a push button and a textedit, i need to write line x line using a loop in a textedit when button is clicked.
The problem is that i need to implement threads because without them the program crashes or it doesn't write line x line, but all together when the loop ends;
and i need to implement in my application a textedit that shows info while the program loads "something".
So my question is, how to rewrite this code using threads to write in textedit when button is clicked?
The method i should run when the button is clicked is "write"
it should work like a print inside a for loop , it would print not all at once
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(150, 220, 321, 71))
self.textEdit.setObjectName("textEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(240, 100, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton.clicked.connect(self.write)
def write(self):
string=""
for i in range(1000):
string="\n"+str(i)
self.textEdit.insertPlainText(string)
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
First of all it is recommended not to modify the class generated by Qt Designer so you must regenerate the file and assume that it is called mainwindow.py: pyuic5 your_file.ui -o mainwindow.py -x.
Considering the above, the logic is to generate the information in another thread and send it to the GUI to modify it:
main.py
import threading
from PyQt5 import QtCore, QtGui, QtWidgets
from mainwindow import Ui_MainWindow
class WriterWorker(QtCore.QObject):
textChanged = QtCore.pyqtSignal(str)
def start(self):
threading.Thread(target=self._execute, daemon=True).start()
def _execute(self):
string = ""
for i in range(1000):
string = "\n"+str(i)
self.textChanged.emit(string)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.writer_worker = WriterWorker()
self.writer_worker.textChanged.connect(self.on_text_changed)
self.pushButton.clicked.connect(self.writer_worker.start)
#QtCore.pyqtSlot(str)
def on_text_changed(self, text):
self.textEdit.insertPlainText(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Code has been edited per requests
I have a program where one menu will link to another menu and then you can perform functions from there.
When I add a hide MainWindow function to the second window, so it goes into the background while you follow the prompts, it will crash, IF you run the whole script.
Ex: Run the main script, click on investigations and then click on any of the menu items.
If I run the script by itself, without going through the main menu, it will run with no issues.
The issue lies within the Mainwindow.hide(), because I can comment this out in the second menu- InvMenu, and I can run the program from the MainMenu and follow the prompts with no issues.
I'm not sure how to fix this. I've ran through quite a few things online and none seem to work.
Here's the code-
Test.py
from PyQt5 import QtCore, QtGui, QtWidgets
from Test2 import *
class Ui_MainWindow(object):
def ipDombtn(self):
self.window=QtWidgets.QMainWindow()
self.ui=Ui_MainWindow1()
self.ui.setup(self.window)
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(412, 440)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.pushButton.clicked.connect(self.ipDombtn)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 412, 31))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Investigation"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Here is the second menu's code. It's set to call specific functions based on what they select. If I comment out the MainWindow.hide() I can run the MainMenu, select investigations and then click on one of the options and they work. If I run this code without running the main menu, and nothing commented out, it works just fine.
Is it an issue with two menus, even though they are renamed?
Test2.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow1(object):
def exIpDomInv_clicked(self):
MainWindow1.hide()
print("You clicked a button...")
MainWindow1.show()
def setup(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(412, 440)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.exIpDomInv = QtWidgets.QPushButton(self.centralwidget)
self.exIpDomInv.setObjectName("exIpDomInv")
self.verticalLayout.addWidget(self.exIpDomInv)
MainWindow.setCentralWidget(self.centralwidget)
self.Menu = QtWidgets.QMenuBar(MainWindow)
self.Menu.setGeometry(QtCore.QRect(0, 0, 412, 31))
self.Menu.setObjectName("Menu")
MainWindow.setMenuBar(self.Menu)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.exIpDomInv.clicked.connect(self.exIpDomInv_clicked)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Investigation"))
MainWindow.setAccessibleName(_translate("MainWindow", "InvestigationMenu"))
self.exIpDomInv.setText(_translate("MainWindow", "External IP/ Domain Investigation"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow1 = QtWidgets.QMainWindow()
ui2 = Ui_MainWindow1()
ui2.setup(MainWindow1)
MainWindow1.show()
sys.exit(app.exec_())
Note that the program is broken does not help, you are probably using some IDE, many times the IDEs do not handle these types of errors correctly so I recommend you execute it in a CMD, if you do so you will get the following message:
Traceback (most recent call last):
File "/path/of/Test2.py", line 6, in exIpDomInv_clicked
MainWindow1.hide()
NameError: name 'MainWindow1' is not defined
Aborted (core dumped)
Well, more or less, it gives us an idea of the problem, it tells us that MainWindow1 is not defined, and this leads me to think that you are not clear about some concepts:
The expression if __name__ == "__main__": can have all the files but only the first one is observed so that the if __name__ == "__main__": of Test2.py will not run and therefore the MainWindow1 that you assume exists will not exist.
Classes are abstractions ie they describe a behavior of their objects, besides they only access the members of their scope, that is, it is like a black box, if the object is not defined within the class it can not be accessed, so if you want to access the Ui_MainWindow of Test.py you can not do it directly as it is, there are several methods to achieve it but it is based on the above.
On the other hand the classes that generate Qt Designer are recommended not to edit them because if you modify something in the .ui and recompile it will erase all the changes, besides they are not widgets so you have limitations such as overwriting of the methods. For more information read this.
Considering the above, I am going to divide it into several files:
ui_mainwindow1.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow1(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(412, 440)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.exIpDomInv = QtWidgets.QPushButton(self.centralwidget)
self.exIpDomInv.setObjectName("exIpDomInv")
self.verticalLayout.addWidget(self.exIpDomInv)
MainWindow.setCentralWidget(self.centralwidget)
self.Menu = QtWidgets.QMenuBar(MainWindow)
self.Menu.setGeometry(QtCore.QRect(0, 0, 412, 31))
self.Menu.setObjectName("Menu")
MainWindow.setMenuBar(self.Menu)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Investigation"))
MainWindow.setAccessibleName(_translate("MainWindow", "InvestigationMenu"))
self.exIpDomInv.setText(_translate("MainWindow", "External IP/ Domain Investigation"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow1 = QtWidgets.QMainWindow()
ui2 = Ui_MainWindow1()
ui2.setup(MainWindow1)
MainWindow1.show()
sys.exit(app.exec_())
ui_mainwindow.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(412, 440)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 412, 31))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Investigation"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
main.py
from PyQt5 import QtWidgets
from ui_mainwindow1 import Ui_MainWindow1
from ui_mainwindow import Ui_MainWindow
class MainWindow1(QtWidgets.QMainWindow, Ui_MainWindow1):
def __init__(self, parent=None):
super(MainWindow1, self).__init__(parent)
self.setupUi(self)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.other_window = MainWindow1()
self.pushButton.clicked.connect(self.other_window.show)
self.other_window.exIpDomInv.clicked.connect(self.hide)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
As you see the object other_window is in the same scope of the class so it can be accessed and therefore all the elements are defined. Also each file has its if __name__ == "__main__":, these serve to test the operation of each class, but only the one in the main.py will be executed, the others will be ignored.
I am currently trying to create a main window that can interact with a dialog window, but I came across an issue I thought was interesting.
Basically, I'm trying to get test_1 to interact with test_2. When I test the code by itself, works, and does what it's supposed to do, but when I use Test_1 to interact with Test_2, the script regarding test 2 no longer has its full functionality.
Interestingly enough, when I go through the code, it's able to print "hello", and the function that I link to print("hello") isn't tied to a class. However, the function that doesn't work (the function where I just ask to insert "hello" into a text box) is tied to a class and that is the function that is not working. Also, the code works just fine if I use pdb within the Test_1.
Here is my code regarding test 1 and test 2 (you'll probably have to install PyQT5 if you haven't already):
Test_1
import test_2 as t2
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Test_1(object):
def setupUi(self, Test_1):
Test_1.setObjectName("Test_1")
Test_1.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(Test_1)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(130, 120, 461, 281))
self.pushButton.setObjectName("pushButton")
Test_1.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(Test_1)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
Test_1.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Test_1)
self.statusbar.setObjectName("statusbar")
Test_1.setStatusBar(self.statusbar)
self.retranslateUi(Test_1)
QtCore.QMetaObject.connectSlotsByName(Test_1)
self.pushButton.clicked.connect(self.connect_test_2)
def retranslateUi(self, Test_1):
_translate = QtCore.QCoreApplication.translate
Test_1.setWindowTitle(_translate("Test_1", "MainWindow"))
self.pushButton.setText(_translate("Test_1", "Open Dialog"))
def connect_test_2(self):
#import pdb; pdb.set_trace()
self.t2 = QtWidgets.QDialog()
ui = t2.Ui_Test_2()
ui.setupUi(self.t2)
self.t2.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Test_1()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Test 2
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Test_2(object):
def setupUi(self, Test_2):
Test_2.setObjectName("Test_2")
Test_2.resize(400, 300)
self.buttonBox = QtWidgets.QDialogButtonBox(Test_2)
self.buttonBox.setGeometry(QtCore.QRect(290, 20, 81, 241))
self.buttonBox.setOrientation(QtCore.Qt.Vertical)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.pushButton = QtWidgets.QPushButton(Test_2)
self.pushButton.setGeometry(QtCore.QRect(20, 90, 341, 161))
self.pushButton.setObjectName("pushButton")
self.textEdit = QtWidgets.QTextEdit(Test_2)
self.textEdit.setGeometry(QtCore.QRect(20, 10, 104, 71))
self.textEdit.setObjectName("textEdit")
self.retranslateUi(Test_2)
#self.buttonBox.accepted.connect(Test_2.accept)
#self.buttonBox.rejected.connect(Test_2.reject)
QtCore.QMetaObject.connectSlotsByName(Test_2)
self.pushButton.clicked.connect(self.printHello)
self.pushButton.clicked.connect(dummyCode)
def printHello(self):
self.textEdit.setText("Hello")
def retranslateUi(self, Test_2):
_translate = QtCore.QCoreApplication.translate
Test_2.setWindowTitle(_translate("Test_2", "Dialog"))
self.pushButton.setText(_translate("Test_2", "PushButton"))
def dummyCode():
print("Hello")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Test_2()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Got stuck with accessing to outer class functions, it'll be great to receive some advice or solution how can i get access to another class method in Python.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(480, 640)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(320, 520, 114, 32))
self.pushButton.setObjectName("pushButton")
#self.pushButton.clicked.connect(self.btnCheck)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(190, 520, 114, 32))
self.pushButton_2.setObjectName("pushButton_2")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 150, 411, 311))
font = QtGui.QFont()
font.setPointSize(66)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton1"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton2"))
self.label.setText(_translate("MainWindow", "TextLabel"))
class Ui_Support(object):
def __init__(self):
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def btnCheck(self):
self.ui.label.setText(a)
def closeBtn(self):
QtCore.QCoreApplication.instance().quit()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
This is my test form, i'd like to call button event from another class, it's easy to call it from inner class function, but it's better to contain multiple functions in another class, thank you for the reply!
The setupUi() function requires an object of type QMainWindow, in your case the optimum would be that Ui_Support should inherit from QMainWindow.
class Ui_Support(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self, parent=None)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.btnCheck)
def btnCheck(self):
self.ui.label.setText("a")
def closeBtn(self):
QtCore.QCoreApplication.instance().quit()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = Ui_Support()
MainWindow.show()
sys.exit(app.exec_())