Skip to content

Instantly share code, notes, and snippets.

@mdmitry1
Last active February 23, 2024 15:56
Show Gist options
  • Save mdmitry1/45c9c9736461fe322e070f7506fbe1bf to your computer and use it in GitHub Desktop.
Save mdmitry1/45c9c9736461fe322e070f7506fbe1bf to your computer and use it in GitHub Desktop.
Platform Independent Qt GUI example
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="windowModality">
<enum>Qt::ApplicationModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1265</width>
<height>708</height>
</rect>
</property>
<property name="baseSize">
<size>
<width>600</width>
<height>600</height>
</size>
</property>
<property name="windowTitle">
<string>Click PushButton to load picture</string>
</property>
<property name="modal">
<bool>true</bool>
</property>
<widget class="QScrollArea" name="imageScrollArea">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<widget class="QLabel" name="imageLabel">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>285</width>
<height>171</height>
</rect>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="styleSheet">
<string notr="true">font: 20pt &quot;MS Shell Dlg 2&quot;;</string>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="text">
<string/>
</property>
<property name="textFormat">
<enum>Qt::AutoText</enum>
</property>
<property name="scaledContents">
<bool>false</bool>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</widget>
</widget>
<widget class="QPushButton" name="btn">
<property name="geometry">
<rect>
<x>850</x>
<y>120</y>
<width>100</width>
<height>40</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
from sys import argv, exit
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from re import sub
from os.path import realpath, dirname
from os import name as osname
class photo(QtWidgets.QDialog):
def __init__(self,parent=None):
super(photo, self).__init__(parent)
form_path=dirname(realpath(argv[0]))+ "/LoadPicture.ui"
if 'nt' == osname:
form_path=sub("/",r"\\", form_path)
uic.loadUi(form_path, self)
self.btn.clicked.connect(self.attachImage)
self.imageWidgetAdded=False
def attachImage(self):
if not self.imageWidgetAdded:
lay = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
lay.setContentsMargins(0, 0, 0, 0)
lay.addWidget(self.imageLabel)
self.imageWidgetAdded=True
image_path=dirname(realpath(argv[0]))+ "/EilatBay.jpg"
if 'nt' == osname:
image_path=sub("/",r"\\", image_path)
pixMap = QtGui.QPixmap(image_path)
self.imageLabel.setPixmap(pixMap)
self.imageScrollArea.setWidgetResizable(True)
self.imageLabel.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
def main():
app = QtWidgets.QApplication(argv)
window = photo()
#window.attachImage()
window.show()
exit(app.exec_())
if __name__ == '__main__':
main()
#!/usr/bin/tcsh -f
set script_realpath=`realpath $0`
set script_path=$script_realpath:h
set python_script_fullpath=${script_path}/pyqt5_image_ex.py
if(`uname` =~ *NT*) then
/ucrt64/bin/python3 $python_script_fullpath
else
/usr/bin/python3.11 $python_script_fullpath
endif
#!/usr/bin/bash -f
script_realpath=$(realpath $0)
script_path=$(dirname $script_realpath)
python_script_fullpath=$script_path/pyqt5_image_ex.py
os=$(uname)
if [[ $os =~ .*NT.* ]]; then
/c/msys64/ucrt64/bin/python3 $python_script_fullpath
else
/usr/bin/python3.11 $python_script_fullpath
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment