I'm trying to make a simple widget with three buttons where for each button when clicked will show the corresponding other widgets.
Following is the code I'm trying to run but cant figure out why the new widgets are not showing.
from PyQt4 import QtGui, QtCore
from functools import partial
import sys
class MainWidget(QtGui.QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.main_widget()
def main_widget(self):
another = Another()
simple = Simple()
grid = QtGui.QGridLayout()
show_another_button = QtGui.QPushButton("Show Another")
show_another_button.clicked.connect(another.show_another)
grid.addWidget(show_another_button, 0, 0)
show_simple_button = QtGui.QPushButton("Show Simple")
show_simple_button.clicked.connect(simple.show_simple)
grid.addWidget(show_simple_button, 0, 1)
print_button = QtGui.QPushButton("Print Hello")
print_button.clicked.connect(partial(print, "Hello"))
grid.addWidget(another_button, 0, 2)
self.setLayout(grid)
self.show()
class Another(QtGui.QWidget):
def __init__(self):
print("another initialized")
super(Another, self).__init__()
def show_another(self):
print("another called")
grid = QtGui.QGridLayout()
self.setLayout(grid)
self.show()
class Simple(QtGui.QDialog):
def __init__(self):
print("simple initialized")
super(Simple, self).__init__()
def show_simple(self):
print("simple called")
self.show()
def main():
app = QtGui.QApplication(sys.argv)
main_widget = MainWidget()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Please Help!
Modified your code slightly. The below works on my system.
from PyQt4 import QtGui
from functools import partial
import sys
class MainWidget(QtGui.QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.another = Another()
self.simple = Simple()
self.grid = QtGui.QGridLayout()
self.main_widget()
def main_widget(self):
show_another_button = QtGui.QPushButton("Show Another")
show_another_button.clicked.connect(self.another.show_form)
self.grid.addWidget(show_another_button, 0, 0)
show_simple_button = QtGui.QPushButton("Show Simple")
show_simple_button.clicked.connect(self.simple.show_simple)
self.grid.addWidget(show_simple_button, 0, 1)
another_button = QtGui.QPushButton("Print Hello")
another_button.clicked.connect(partial(print, "Hello"))
self.grid.addWidget(another_button, 0, 2)
self.setLayout(self.grid)
class Another(QtGui.QWidget):
def __init__(self):
super(Another, self).__init__()
print("another initialized")
def show_form(self):
print("another called")
grid = QtGui.QGridLayout()
self.setLayout(grid)
self.show()
class Simple(QtGui.QDialog):
def __init__(self):
super(Simple, self).__init__()
print("simple initialized")
def show_simple(self):
print("simple called")
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = MainWidget()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Please note: If you try to launch Another() when the Widget is still open you will get an error:
QWidget::setLayout: Attempting to set QLayout "" on Another "", which already has a layout
Related
When I run the code below my program crashes, and I believe this is to-do with the .text() called when the Line edit has something typed in it. I need to assign a variable to what is entered here.
import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
class loginScreen(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
usernameBox = QtWidgets.QLineEdit()
usernameBox.textChanged.connect(self.myfunc)
vArrangement = QtWidgets.QVBoxLayout()
vArrangement.addWidget(usernameBox)
self.setLayout(vArrangement)
self.show()
def myfunc(self):
x = usernameBox.text()
print(x)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = loginScreen()
sys.exit(app.exec_())
If you observe usernameBox it is created as a local variable so it can not be accessed by the other methods of the class, in your case there are 2 solutions:
Make a usernameBox attribute of the class.
class loginScreen(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.usernameBox = QtWidgets.QLineEdit()
self.usernameBox.textChanged.connect(self.myfunc)
vArrangement = QtWidgets.QVBoxLayout()
vArrangement.addWidget(self.usernameBox)
self.setLayout(vArrangement)
self.show()
def myfunc(self):
x = self.usernameBox.text()
print(x)
Or use sender() that obtains the object that emits the signal, in your case the QLineEdit.
class loginScreen(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
usernameBox = QtWidgets.QLineEdit()
usernameBox.textChanged.connect(self.myfunc)
vArrangement = QtWidgets.QVBoxLayout()
vArrangement.addWidget(usernameBox)
self.setLayout(vArrangement)
self.show()
def myfunc(self):
x = self.sender().text()
print(x)
import sys, os
import PyQt4
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Tab1Widget1(QWidget):
def __init__(self, parent=None):
super().__init__()
self.Tab1Widget1initUI()
self.bridge = Tab1Widget2()
def Tab1Widget1initUI(self):
self.setLayout(QGridLayout())
self.T1W1_checkbox = QCheckBox('checkbox1', self)
self.layout().addWidget(self.T1W1_checkbox, 1, 0)
def test(self):
print ('123')
def run(self):
if self.T1W1_checkbox.isChecked() == True:
self.test()
if self.bridge.T1W2_checkbox.isChecked() == True:
print (self.bridge.T1W2_le.text())
class Tab1Widget2(QWidget):
def __init__(self, parent=None):
super().__init__()
self.setLayout(QGridLayout())
self.T1W2_checkbox = QCheckBox('checkbox2', self)
self.layout().addWidget(self.T1W2_checkbox, 0, 0)
self.T1W2_le = QLineEdit()
self.layout().addWidget(self.T1W2_le, 0, 1)
class Tab1Layout(QWidget):
def __init__(self, parent=None):
super().__init__()
self.setLayout(QGridLayout())
self.group1 = Tab1Widget1(self)
scroll = QScrollArea(self)
scroll.setWidget(self.group1)
scroll.setWidgetResizable(True)
self.layout().addWidget(scroll, 0, 0)
self.group2 = Tab1Widget2(self)
self.layout().addWidget(self.group2, 1, 0)
self.btnRun = QPushButton('Run', self)
self.layout().addWidget(self.btnRun, 3, 0)
self.btnRun.clicked.connect(self.group1.run)
class Page1(QTabWidget):
def __init__(self, parent=None):
super().__init__()
self.tab1 = Tab1Layout()
self.addTab(self.tab1, "Tab1")
self.tab2 = QWidget()
self.tab3 = QWidget()
self.addTab(self.tab2, "Tab2")
self.addTab(self.tab3, "Tab3")
self.tab2_initUI()
self.tab3_initUI()
def tab2_initUI(self):
grid = QGridLayout()
self.tab2.setLayout(grid)
def tab3_initUI(self):
grid = QGridLayout()
self.tab3.setLayout(grid)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__()
self.setGeometry(450, 250, 800, 550)
self.startPage1()
def startPage1(self):
x = Page1(self)
self.setWindowTitle("Auto Benchmark")
self.setCentralWidget(x)
self.show()
def main():
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
If checkbox1 is checked and I press the run button, it will print 123. However, by pressing run button, I want checkbox2 to also print some texts entered in lineedit if the checkbox1 are also checked (i.e. it should print 123 first and then print 456).
I've looked up some similar types of questions, but none of that provides a proper answer. If anyone knows how to solve it, pls let me know thanks!!
The problem is that you are creating several Tab1Widget2, the first one you created in Tab1Layout, and that is the one you see, then you have created another one in Tab1Widget1, but not the time because you have not passed a parent, if you pass it to ** self ** as parent you will observe the following:
self.bridge = Tab1Widget2(self)
which is not what you want, so instead of creating a new you must pass the one that already exists, an option is to pass it through the constructor:
class Tab1Widget1(QWidget):
def __init__(self, bridge, parent=None): # Modify here
super().__init__(parent)
self.Tab1Widget1initUI()
self.bridge = bridge #Modify here
# ...
def test(self): print ('123')
def run(self):
if self.T1W1_checkbox.isChecked():
self.test()
if self.bridge.T1W2_checkbox.isChecked():
print (self.bridge.T1W2_le.text())
class Tab1Widget2(QWidget):
# ...
class Tab1Layout(QWidget):
def __init__(self, parent=None):
super().__init__()
self.setLayout(QGridLayout())
self.group2 = Tab1Widget2(self) # Modify here
self.group1 = Tab1Widget1(self.group2, self) # Modify here
scroll = QScrollArea(self)
scroll.setWidget(self.group1)
scroll.setWidgetResizable(True)
self.layout().addWidget(scroll, 0, 0)
# self.group2 = Tab1Widget2(self) # Modify here
self.layout().addWidget(self.group2, 1, 0)
# ...
Getting very stuck here, I am trying to learn how to use classes, and so simply want to show a button on a window when the button is in a different class. Here is the code I am trying to use:
#!/usr/bin/python3
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(0, 0, 800, 600)
main_menu = Menu()
self.show()
class Menu(QtGui.QWidget):
def __init__(self):
btn = QtGui.QPushButton("Quit")
btn.resize(btn.sizeHint())
btn.move(100,100)
btn.show()
print("Hello I am a menu")
def main():
app = QtGui.QApplication(sys.argv)
main_window = Window()
sys.exit(app.exec())
if __name__ == "__main__":
main()
This works so far as I get a window, but no button on it, howerver the print message I put in works. What am I doing wrong please?
You must initialize the parent class in Menu, in addition to passing the parent to that class and the button.
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(0, 0, 800, 600)
main_menu = Menu(self)
self.show()
class Menu(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
btn = QtGui.QPushButton("Quit", self)
btn.resize(btn.sizeHint())
btn.move(100,100)
print("Hello I am a menu")
def main():
app = QtGui.QApplication(sys.argv)
main_window = Window()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Moving on from my last question, I'm stuck once again. I'm trying to update content of parent widget from child widget. The code seems to work first time but after closing and re-opening the form widget it does not update the parent widget.
Following is the code.
from PyQt4 import QtGui, QtCore
from functools import partial
import sys
class MainWidget(QtGui.QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.main_widget()
def main_widget(self):
self.form = Form()
self.simple = Simple()
grid = QtGui.QGridLayout()
self.last_input_label = QtGui.QLabel("")
grid.addWidget(self.last_input_label, 1, 0, 3, 1)
show_form_button = QtGui.QPushButton("Show Form")
show_form_button.clicked.connect(partial(self.form.show_form, self.last_input_label))
grid.addWidget(show_form_button, 0, 0)
show_simple_button = QtGui.QPushButton("Show Simple")
show_simple_button.clicked.connect(self.simple.show_simple)
grid.addWidget(show_simple_button, 0, 1)
another_button = QtGui.QPushButton("Print Hello")
another_button.clicked.connect(partial(print, "Hello"))
grid.addWidget(another_button, 0, 2)
self.setLayout(grid)
self.setWindowTitle("Main Widget")
self.show()
def closeEvent(self, QCloseEvent):
QtGui.QApplication.closeAllWindows()
class Form(QtGui.QWidget):
def __init__(self):
print("form initialized")
super(Form, self).__init__()
def show_form(self, last_input_label):
print("form called")
grid = QtGui.QGridLayout()
self.last_input_label = last_input_label
label = QtGui.QLabel("Name")
grid.addWidget(label, 0, 0)
self.line_edit = QtGui.QLineEdit()
grid.addWidget(self.line_edit, 0, 1)
self.submit_button = QtGui.QPushButton("Submit")
self.submit_button.clicked.connect(self.print_form_data)
grid.addWidget(self.submit_button, 1, 1)
self.setLayout(grid)
self.setGeometry(250, 300, 250, 150)
self.setWindowTitle("Form Widget")
self.show()
def get_form_data(self):
form_data = {
"name": self.line_edit.text()
}
return form_data
def print_form_data(self):
self.x = self.get_form_data()
for item in self.x:
print(item + ": " + self.x[item])
self.last_input_label.setText(self.x[item])
return
class Simple(QtGui.QDialog):
def __init__(self):
print("simple initialized")
super(Simple, self).__init__()
def show_simple(self):
print("simple called")
self.setGeometry(300, 250, 250, 150)
self.setWindowTitle("Simple Widget")
self.show()
def main():
app = QtGui.QApplication(sys.argv)
main_widget = MainWidget()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Please Help!
You're calling the initialization code each time you show the widget. Move all that into the __init__ method where it belongs and it all works.
Move everything beside this into the init method. I can't say exactly why running the init code more would break the connection. But somehow it does. Maybe someone else can fill in that detail.
def show_form(self, last_input_label):
print("form called")
self.last_input_label = last_input_label
self.show()
I'm trying to set the focus on QLineEdit widget at app startup but for some reasons it fails. Calling the method which includes the QLineEdit_object.setFocus() and is bound to a button click, works perfectly. However on startup, it seems like it doesn't execute at all when set to initialize after widget creation.
Using PySide with Python.
# coding=utf-8
import sys
import PySide.QtGui as QG
import PySide.QtCore as QC
class GG(QG.QMainWindow):
def __init__(self):
super(GG, self).__init__()
self.move(0,0)
self.resize(400,300)
self.setWindowTitle('Demo')
self.tabw = QG.QTabWidget()
self.tab1 = Tab1()
self.tab2 = Tab2()
self.tabw.addTab(self.tab1, 'Tab1')
self.tabw.addTab(self.tab2, 'Tab2')
hbox = QG.QHBoxLayout()
hbox.addWidget(self.tabw)
self.setCentralWidget(self.tabw)
self.setLayout(hbox)
self.show()
class Tab1(QG.QWidget):
def __init__(self):
super(Tab1, self).__init__()
self.btns()
self.inputt()
self.layoutz()
self.inp.setFocus() # doesn't set the focus on startup ?
self.show()
def inputt(self):
self.inp = QG.QLineEdit('', self)
def btns(self):
self.btn1 = QG.QPushButton('Button1', self)
self.btn1.clicked.connect(self.focusit) # works just fine
def layoutz(self):
vbox = QG.QVBoxLayout()
vbox.addWidget(self.btn1)
vbox.addStretch(1)
vbox.addWidget(self.inp)
self.setLayout(vbox)
def focusit(self):
self.inp.setFocus() # works just fine
class Tab2(Tab1):
def __init__(self):
super(Tab2, self).__init__()
def main():
app = QG.QApplication(sys.argv)
a = GG()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Well, after some playing I came up with this solution:
import sys
import PySide.QtGui as QG
import PySide.QtCore as QC
class GG(QG.QMainWindow):
def __init__(self):
super(GG, self).__init__()
self.move(0,0)
self.resize(400,300)
self.setWindowTitle('Demo')
self.tabw = QG.QTabWidget()
self.tab1 = Tab1()
self.tab2 = Tab2()
self.tabw.addTab(self.tab1, 'Tab1')
self.tabw.addTab(self.tab2, 'Tab2')
hbox = QG.QHBoxLayout()
hbox.addWidget(self.tabw)
self.setCentralWidget(self.tabw)
self.setLayout(hbox)
self.tab2.inp.setFocus() # setting focus right here
self.tab1.inp.setFocus() # and here; notice the order
self.show()
class Tab1(QG.QWidget):
def __init__(self):
super(Tab1, self).__init__()
self.btns()
self.inputt()
self.layoutz()
self.show()
def inputt(self):
self.inp = QG.QLineEdit('', self)
def btns(self):
self.btn1 = QG.QPushButton('Button1', self)
self.btn1.clicked.connect(self.focusit)
def layoutz(self):
vbox = QG.QVBoxLayout()
vbox.addWidget(self.btn1)
vbox.addStretch(1)
vbox.addWidget(self.inp)
self.setLayout(vbox)
def focusit(self):
self.inp.setFocus()
class Tab2(Tab1):
def __init__(self):
super(Tab2, self).__init__()
def main():
app = QG.QApplication(sys.argv)
a = GG()
sys.exit(app.exec_())
if __name__ == '__main__':
main()