Skip to content

Instantly share code, notes, and snippets.

@Endogen
Created September 27, 2017 14:27
Show Gist options
  • Save Endogen/7bc8cec2296f4603e48f2269702f9d98 to your computer and use it in GitHub Desktop.
Save Endogen/7bc8cec2296f4603e48f2269702f9d98 to your computer and use it in GitHub Desktop.
Python3 GUI with PyQt5
#!/usr/bin/python3
import sys
import platform
from PyQt5.QtWidgets import (QWidget, QToolTip, QPushButton, QApplication, QMessageBox, QDesktopWidget)
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import QFont
# TODO: Check with 'check_system' on which system we are and if on windows, do this:
# https://stackoverflow.com/questions/1551605/how-to-set-applications-taskbar-icon-in-windows-7
# http://doc.qt.io/qt-5/appicon.html
class Example(QWidget):
# Constructur
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
QToolTip.setFont(QFont('SansSerif', 10))
self.setToolTip('This is a <b>QWidget</b> widget')
# Sample button
btn = QPushButton('Button', self)
btn.setToolTip('This is a <b>QPushButton</b> widget')
btn.resize(btn.sizeHint())
btn.move(10, 10)
# Quit button
qbtn = QPushButton('Quit', self)
qbtn.setToolTip("This button will close the window")
qbtn.clicked.connect(QCoreApplication.instance().quit)
qbtn.resize(qbtn.sizeHint())
qbtn.move(10, 40)
#self.resize(250, 100) # Größe
#self.move(300, 300) # Position
self.setGeometry(300, 300, 300, 200) # Position & Größe
self.setWindowTitle('My Qt5 window')
self.center()
self.show()
# Center frame on screen
def center(self):
# Rectangle specifying the geometry of the main window
qr = self.frameGeometry()
# 'QDesktopWidget' provides information about the user's desktop
# Figure out screen resolution of monitor and from this resolution, get center point
cp = QDesktopWidget().availableGeometry().center()
# Set the center of the rectangle to the center of the screen
qr.moveCenter(cp)
# Move top-left point of window to top-left point of qr rectangle, thus centering the window on screen
self.move(qr.topLeft())
# Overwrite event to close window
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
# Check on which system we are running
def check_system():
if platform.system() == "Linux":
print("linux")
elif platform.system() == "Darwin":
print("osx")
elif platform.system() == "Windows":
print("windows")
# MAIN method(?)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment