Skip to content
Snippets Groups Projects

Implemented a GUI framework

Merged Felix Goding requested to merge 44-create-basic-gui into develop
All threads resolved!
1 file
+ 66
44
Compare changes
  • Side-by-side
  • Inline
+ 66
44
"""
This python file is an example of how a GUI can be implemented
using buttons and textboxes.
"""
import sys
from PyQt5.QtWidgets import QApplication,QWidget ,QMainWindow, QLabel, QToolBar, QAction, QStatusBar, QHBoxLayout, QVBoxLayout, QCheckBox, QMenu, QMenuBar, QMenuBar, QLineEdit, QPushButton
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QIcon, QFont, QPainter, QBrush, QPen
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QLabel, QAction,\
QStatusBar, QMenuBar, QLineEdit, QPushButton
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QFont, QPainter, QPen
"""
https://stackoverflow.com/questions/12219727/dragging-moving-a-qpushbutton-in-pyqt
Hur man skapar en dragbar knapp
"""
class DragButton(QPushButton):
"""How to create a dragbutton
https://stackoverflow.com/questions/12219727/dragging-moving-a-qpushbutton-in-pyqt
"""
def mousePressEvent(self, event):
self.__mouse_press_pos = None
self.__mouse_move_pos = None
@@ -22,7 +25,6 @@ class DragButton(QPushButton):
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
# adjust offset from clicked point to origin of widget
cur_pos = self.mapToGlobal(self.pos())
global_pos = event.globalPos()
diff = global_pos - self.__mouse_move_pos
@@ -35,33 +37,35 @@ class DragButton(QPushButton):
def mouseReleaseEvent(self, event):
if self.__mouse_press_pos is not None:
moved = event.globalPos() - self.__mouse_press_pos
moved = event.globalPos() - self.__mouse_press_pos
if moved.manhattanLength() > 3:
event.ignore()
return
super(DragButton, self).mouseReleaseEvent(event)
class subwindow(QWidget):
def createWindow(self,WindowWidth,WindowHeight):
parent=None
super(subwindow,self).__init__(parent)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.resize(WindowWidth,WindowHeight)
class SubWindow(QWidget):
"""Creates a sub window
"""
def create_window(self, window_width, window_height):
"""Creates a window
"""
parent = None
super(SubWindow, self).__init__(parent)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.resize(window_width, window_height)
class MainWindow(QMainWindow):
"""Main window for the program
"""
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle(" ")
self.setWindowIcon(QIcon('small_logo.png'))
test_button = QAction("Test", self)
open_window_button = QPushButton(" Open a new window ", self)
open_window_button.clicked.connect(self.create_sub_window)
open_window_button.resize(150, 40)
open_window_button.move(500, 100)
#Menu buttons
test_button = QAction("Test", self)
exit_button = QAction("Exit", self)
exit_button.setShortcut("Ctrl+Q")
@@ -86,28 +90,43 @@ class MainWindow(QMainWindow):
edit_menu = menu_bar.addMenu("&Edit")
edit_menu.addAction(edit_button)
edit_menu.addSeparator()
view_menu = menu_bar.addMenu("&View")
view_menu.addAction(view_button)
# Drag buttons
addition_button = DragButton("Addition", self)
addition_button.move(10, 130)
addition_button.setFixedSize(70, 20)
addition_button.clicked.connect(self.clicked)
addition_counter = 1
addition_button.clicked.connect(self.create_sub_window)
addition_button2 = DragButton("Addition", self)
addition_button2.move(10, 130)
addition_button2.setFixedSize(70, 20)
addition_button2.clicked.connect(self.create_sub_window)
subtraction_button = DragButton("Subtraction", self)
subtraction_button.move(10, 170)
subtraction_button.setFixedSize(70, 20)
#subtraction_button.clicked.connect(clicked)
subtraction_button.clicked.connect(self.create_sub_window)
subtraction_button2 = DragButton("Subtraction", self)
subtraction_button2.move(10, 170)
subtraction_button2.setFixedSize(70, 20)
subtraction_button2.clicked.connect(self.create_sub_window)
multiplication_button = DragButton("Multiplication", self)
multiplication_button.move(10, 210)
multiplication_button.setFixedSize(70, 20)
#multiplication_button.clicked.connect(clicked)
multiplication_button.clicked.connect(self.create_sub_window)
multiplication_button2 = DragButton("Multiplication", self)
multiplication_button2.move(10, 210)
multiplication_button2.setFixedSize(70, 20)
multiplication_button2.clicked.connect(self.create_sub_window)
self.setStatusBar(QStatusBar(self))
def on_file_button_click(self):
@@ -123,23 +142,35 @@ class MainWindow(QMainWindow):
QApplication.quit()
def clicked(self):
print ("Drag button clicked")
print("Drag button clicked")
def paintEvent(self, e):
# Temporary black box for operations
painter = QPainter(self)
painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
painter.drawRect(0, 110, 100, 400)
# Temporary arrow resembling a signal
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(Qt.black)
painter.setBrush(Qt.white)
painter.drawLine(300, 200, 400, 200)
painter.drawLine(400, 200, 395, 195)
painter.drawLine(400, 200, 395, 205)
def create_sub_window(self):
self.sub_window = subwindow()
self.sub_window.createWindow(400, 300)
""" Example of how to create a sub window
"""
self.sub_window = SubWindow()
self.sub_window.create_window(400, 300)
self.sub_window.setWindowTitle("Properties")
self.sub_window.properties_label = QLabel(self.sub_window)
self.sub_window.properties_label.setText('Properties')
self.sub_window.properties_label.setFixedWidth(400)
self.sub_window.properties_label.setFont(QFont('SansSerif', 14, QFont.Bold))
self.sub_window.properties_label.setAlignment(Qt.AlignCenter)
self.sub_window.name_label = QLabel(self.sub_window)
self.sub_window.name_label.setText('Name:')
self.sub_window.name_label.move(20, 40)
@@ -158,19 +189,10 @@ class MainWindow(QMainWindow):
self.sub_window.id_line.move(70, 70)
self.sub_window.id_line.resize(100, 20)
"""
vertical_box = QVBoxLayout()
vertical_box.addWidget(self.sub_window.name_label)
vertical_box.addWidget(id_label)
self.sub_window.setLayout(vertical_box)
"""
self.sub_window.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
@@ -180,4 +202,4 @@ if __name__ == "__main__":
window.show()
app.exec_()
\ No newline at end of file
app.exec_()
Loading