So my problem is the following:
I have a program that I created in QT Designer and added some code in Python.
There is an main window and two windows wich appear when you click an button in the window before.
The List Widget is in the second window:
Hope with this picture you can understand what I mean
So now I want to add some Items to my list widget.
def add_number_to_list(self):
self.ui.list_of_numbers.addItem('test')
So I call this function on another place but it didn't happen anything.
If I add an print command, to test if the function works: The print is showed. But nothing appears in the list widget...
What is wrong?
import sys
from PyQt4 import QtGui
import p_design, p_edit_numbers, p_add_number
number_to_add = ''
class Example(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = p_design.Ui_MainWindow()
self.ui.setupUi(self)
self.ui.manually_alarm.clicked.connect(self.alarm)
self.ui.edit_numbers.clicked.connect(self.open_edit_numbers)
def alarm(t1,t2):
#send sms to numbers
print('ALARM!')
def open_edit_numbers(self):
self.test = EditNumbers(self)
self.test.show()
class EditNumbers(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = p_edit_numbers.Ui_MainWindow()
self.ui.setupUi(self)
self.ui.add_number.clicked.connect(self.open_add_number)
### Problem! ###
def add_number_to_list(self):
self.ui.list_of_numbers.addItems('test')
################
def open_add_number(self):
self.t3 = AddNumber(self)
self.t3.show()
class AddNumber(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = p_add_number.Ui_MainWindow()
self.ui.setupUi(self)
self.ui.add.clicked.connect(self.add_number)
def add_number(self):
global number_to_add
editnumbers=EditNumbers()
editnumbers.add_number_to_list()
def main():
app = QtGui.QApplication(sys.argv)
form = Example()
form.show()
sys.exit(app.exec_())
main()
Related
Assume we have an interface with Buttons and LineEdits in one .py file. I have this code in another, which inherits it:
import Inter_Input_Blasting as interf
from PyQt6 import QtCore,QtWidgets,QtGui
from functools import partial
class MainWindow(QtWidgets.QMainWindow):
def on_clicked(self):
print("Button Pushed")
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
self.ui = interf.Ui_MainWindow()
self.ui.setupUi(self)
self.ui.calc_button.clicked.connect(MainWindow.on_clicked)
self.ui.input_overall_1.textChanged.connect(MainWindow.gather_data)
def gather_data(self):
return self.ui.input_overall_1.text()
if __name__== "__main__":
import sys
app = interf.QtWidgets.QApplication(sys.argv)
Form = MainWindow()
Form.show()
sys.exit(app.exec())
So, i need to print my values into console when i put it in the lineedit fiels. The .textChanged() method is working, but the .gather_data() isn't.
Provide a variable for storing the text:
def __init__(self,parent=None):
self.txt = None
Then in method gather_data, store the text in that variable:
def gather_data(self):
sef.txt = self.ui.input_overall_1.text()
then before sys.exit, print that value:
r = app.exec()
print(Form.txt)
sys.exit(r)
I have an UI designed in QT Designer. It is formed from three tabs:
1 - Test; 2 - Train Haar; 3 - Train Hog;
In every tabs I have some buttons, or some lines, or some widgets, but when I create the code to add functions to those buttons or widgets I want to have 3 classes for every tabs, one class only for first tab, one class only for second tab and so on..
And a fourth class who call all three classes and compose my UI. I do not know how to do this, I need every classes to inherit from QMainWindow? I need to setupUi in every class?
This is my current code:
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from Qt_designer_UI import Ui_MainWindow
class Test(QtWidgets.QMainWindow):
def __init__(self):
super(Test, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Train_Haar(QtWidgets.QMainWindow):
def __init__(self):
super(Train_Haar, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Train_HOG(QtWidgets.QMainWindow):
def __init__(self):
super(Train_HOG, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Compose_UI(QtWidgets.QMainWindow):
def __init__(self):
super(Compose_UI, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
Test()
Train_Haar()
Train_HOG
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Compose_UI()
window.show()
sys.exit(app.exec_())
I need every classes to inherit from QMainWindow?
No, it is not necessary since it is a widget that will be inside the tabs, and that widget can be of any type.
I need to setupUi in every class?
It is only obligatory to call setupUi if you use a generated class with the help of pyuic and Qt Designer, in your case the widgets that are in each tab are generated with Qt Designer? I see that I do not
Keeping in mind that your .ui is
a possible solution is:
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from Qt_designer_UI import Ui_MainWindow
class Test(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Test, self).__init__(parent)
# for testing
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(QtWidgets.QPushButton("Test"))
class Train_Haar(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Train_Haar, self).__init__(parent)
# for testing
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(QtWidgets.QPushButton("Train_Haar"))
class Train_HOG(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Train_HOG, self).__init__(parent)
# for testing
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(QtWidgets.QPushButton("Train_HOG"))
class Compose_UI(QtWidgets.QMainWindow):
def __init__(self):
super(Compose_UI, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
test = Test()
train_haar = Train_Haar()
train_hog = Train_HOG()
for w, tab in zip(
(test, train_haar, train_hog), (self.ui.tab1, self.ui.tab2, self.ui.tab3)
):
lay = QtWidgets.QVBoxLayout(tab)
lay.addWidget(w)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Compose_UI()
window.show()
sys.exit(app.exec_())
I open the dialog from the main window, where by clamping the keys, I fill the line with their names. The problem is that I can not understand where you need to do a cycle of checking all the keys on their state. Maybe there is another way to get the keys pressed? Or where you need to listen to the clamping so that the dialog box does not hang and the string is updated.
MainWindow:
def showBindings(self, param):
from dialogs import KeyBindingsDialog
self.dialog = KeyBindingsDialog()
self.dialog.show()
Dialog:
class KeyBindingsDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(KeyBindingsDialog, self).__init__(parent)
self.ui = KeyBindings()
self.ui.setupUi(self)
Use QKeySequenceEdit:
from PyQt5 import QtCore, QtGui, QtWidgets
class KeySequenceEdit(QtWidgets.QKeySequenceEdit):
def keyPressEvent(self, event):
super(KeySequenceEdit, self).keyPressEvent(event)
seq_string = self.keySequence().toString(QtGui.QKeySequence.NativeText)
if seq_string:
last_seq = seq_string.split(",")[-1].strip()
le = self.findChild(QtWidgets.QLineEdit, "qt_keysequenceedit_lineedit")
self.setKeySequence(QtGui.QKeySequence(last_seq))
le.setText(last_seq)
self.editingFinished.emit()
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self._keysequenceedit = KeySequenceEdit(editingFinished=self.on_editingFinished)
button = QtWidgets.QPushButton("clear", clicked=self._keysequenceedit.clear)
hlay = QtWidgets.QHBoxLayout(self)
hlay.addWidget(self._keysequenceedit)
hlay.addWidget(button)
#QtCore.pyqtSlot()
def on_editingFinished(self):
sequence = self._keysequenceedit.keySequence()
seq_string = sequence.toString(QtGui.QKeySequence.NativeText)
print("sequence: ", seq_string)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
I am quite new in pyqt design
and I have a question.
I have a Main form. and a child form.
I want the child form to be opened inside the parent form.
how can I do that in python?
I have written a code that just open the form.
but it didnot open it inside the form?
Thanks
##this method creates an Form Main
class FrmMainForm (QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.uiMain = FrmMain.Ui_MainWindow()
self.uiMain.setupUi(self)
action = self.uiMain.menuManual_Mode.addAction(self.tr('ManualMode'))
action.triggered.connect(self.handleNewWindow)
def handleNewWindow(self):
ex = Main(self.uiMain)
ex.show()
class Main(QtGui.QWidget):
white = "QWidget { background-color:#FFFFFF }"
red = "QWidget { background-color:#AB0000}"
green = "QWidget { background-color:#00C000}"
def __init__(self,p):
QtGui.QWidget.__init__(self)
self.p = p
self.initLogAnalyzerManuall()
def initLogAnalyzerManuall(self):
QtGui.QWidget.__init__(self)
self.ui = Ui_Form()
self.ui.setupUi(self,self.p)
The child window will be garbage-collected when the handleNewWindow method returns, and so it will never get the chance to be shown.
You need to keep a reference to the child window, but the way you go about it depends on what you are trying to achieve. Do you literally want a new window to be opened every time the button is clicked? If so, then do this:
def handleNewWindow(self):
ex = Main(self)
ex.show()
class Main(QtGui.QWidget):
...
def __init__(self, parent):
QtGui.QWidget.__init__(self, parent)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.ui = Ui_Form()
self.ui.setupUi(self)
On the other hand, if you want the same child window to be opened every time, then do this:
class FrmMainForm (QtGui.QWidget):
def __init__(self):
...
self._child_window = None
def handleNewWindow(self):
if self._child_window is None:
self._child_window = Main(self)
self._child_window.show()
class Main(QtGui.QWidget):
...
def __init__(self, parent):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
Note that in both cases, you can access the main window from the child window with self.parent().
I have a problem opening new window in my python gui app. I have 3 classes (first login is shown, and than 2 windows are opened). This works fine:
class LoginDialog(QtGui.QDialog):
def __init__(self, parent = None):
super(LoginDialog, self).__init__(parent)
.....
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
.....
class ImageViewerMainWindow(QtGui.QMainWindow):
def __init__(self, path, parent = None):
super(ImageViewerMainWindow, self).__init__(parent)
.....
if __name__ == "__main__":
qtApp = QtGui.QApplication(sys.argv)
loginDlg = LoginDialog()
if not loginDlg.exec_():
sys.exit(-1)
MyMainWindow = MainWindow()
MyMainWindow.show()
viewer = ImageViewerMainWindow("C:\image.jpg")
viewer.show()
sys.exit(qtApp.exec_())
I need viewer to be executed from MainWindow but when I put it like this it just flashes and disappear:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
.....
def DoOpenImageViewer(self):
viewer = ImageViewerMainWindow("C:\image.jpg")
viewer.show()
You need to keep a reference to you viewer, otherwise the new window is destroyed when viewer goes out of scope and is garbage collected.
If you only need one Window at a time, you can do something like:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
.....
def DoOpenImageViewer(self):
self.viewer = ImageViewerMainWindow("C:\image.jpg")
self.viewer.show()
Otherwise you could use a list to store the references.