Skip to content

Instantly share code, notes, and snippets.

@MaurizioB

MaurizioB/answer.py Secret

Last active Aug 15, 2019
Embed
What would you like to do?
StackOverflow rudeness debate
from sys import exit as sysExit
# from PyQt5.QtCore import ??
# from PyQt5.QtGui import ??
# Container Widgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from PyQt5.QtWidgets import QVBoxLayout, QHBoxLayout, QLabel
# Action Widgets
from PyQt5.QtWidgets import QPushButton
class CentralPanel(QWidget):
def __init__(self, parent):
QWidget.__init__(self)
self.Parent = parent
self.Toggle = True
self.lblFooBar = QLabel()
self.lblFooBar.setText('FooBar')
self.btnClick = QPushButton()
self.btnClick.setText('Click')
self.btnClick.clicked.connect(self.setTextHelloWorld)
HBox = QHBoxLayout()
HBox.addWidget(self.btnClick)
HBox.addStretch(1)
VBox = QVBoxLayout()
VBox.addWidget(self.lblFooBar)
VBox.addLayout(HBox)
VBox.addStretch(1)
self.setLayout(VBox)
def setTextHelloWorld(self):
if self.Toggle:
self.Toggle = False
self.lblFooBar.setText("Hello World")
else:
self.Toggle = True
self.lblFooBar.setText("Narf!")
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.title = 'Dialog'
self.setWindowTitle(self.title)
left=100;top=100;width=400;height=300
self.setGeometry(left,top,width,height)
self.CenterPane = CentralPanel(self)
self.setCentralWidget(self.CenterPane)
if __name__ == '__main__':
MainThred = QApplication([])
MainGui = MainWindow()
MainGui.show()
sysExit(MainThred.exec_())
from PyQt5 import QtWidgets
class Central(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
layout = QtWidgets.QGridLayout()
self.setLayout(layout)
self.label = QtWidgets.QLabel('Foo')
layout.addWidget(self.label)
self.button = QtWidgets.QPushButton('Click')
layout.addWidget(self.button)
self.button.clicked.connect(self.labelToggle)
self.status = False
def labelToggle(self):
self.label.setText('Foo' if self.status else 'Bar')
self.status = not self.status
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
central = Central()
self.setCentralWidget(central)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
@MaurizioB

This comment has been minimized.

Copy link
Owner Author

@MaurizioB MaurizioB commented Aug 15, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.