Skip to content

Instantly share code, notes, and snippets.

@sabapathygithub
Last active September 13, 2022 17:54
Show Gist options
  • Save sabapathygithub/2d485b4debdd4d6fefe460a5295163cc to your computer and use it in GitHub Desktop.
Save sabapathygithub/2d485b4debdd4d6fefe460a5295163cc to your computer and use it in GitHub Desktop.
Logic gate code with dynamic image updation
from PyQt5.Qt import *
class TestDialog(QDialog):
def __init__(self, is_poisson=False,
flag=Qt.WindowSystemMenuHint | Qt.WindowTitleHint | Qt.WindowCloseButtonHint):
super(TestDialog, self).__init__(None, flag)
self.setGeometry(0, 0, 400, 300)
grid_layout = QGridLayout()
self.input1 = QCheckBox("Input A")
self.input2 = QCheckBox("Input B")
grid_layout.addWidget(self.input1, 2, 0)
grid_layout.addWidget(self.input2, 6, 0)
self.img_label = QLabel(self)
self.img_label.setBackgroundRole(QPalette.Base)
self.img_label.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
self.img_label.setScaledContents(True)
self.img_label.setPixmap(QPixmap.fromImage(QImage(r"..\images\and_gate.png")))
# addWidget get 5 inputs here (Widget, row, column, rowspan, columnspan)
# Reference: https://stackoverflow.com/questions/61451279/how-does-setcolumnstretch-and-setrowstretch-works
grid_layout.addWidget(self.img_label, 1, 1, 7, 3)
self.output = QCheckBox("Output")
grid_layout.addWidget(self.output, 4, 5)
hori_layout = QHBoxLayout()
and_button = QPushButton("AND")
hori_layout.addWidget(and_button)
and_button.clicked.connect(self.and_button_clicked)
or_button = QPushButton("OR")
hori_layout.addWidget(or_button)
or_button.clicked.connect(self.or_button_clicked)
vert_layout = QVBoxLayout()
vert_layout.addLayout(grid_layout)
vert_layout.addLayout(hori_layout)
self.setLayout(vert_layout)
def and_button_clicked(self):
self.img_label.setPixmap(QPixmap.fromImage(QImage(r"..\images\and_gate.png")))
input1_val = self.input1.isChecked()
input2_val = self.input2.isChecked()
self.output.setChecked(input1_val and input2_val)
def or_button_clicked(self):
self.img_label.setPixmap(QPixmap.fromImage(QImage(r"D:\sabapathygithub\Geologik\images\or_gate.png")))
input1_val = self.input1.isChecked()
input2_val = self.input2.isChecked()
self.output.setChecked(input1_val or input2_val)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment