Buy Me a Coffee? Your support is much appreciated!
PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn
Source Code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QLabel, QShortcut
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QKeySequence
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.window_width, self.window_height = 1200, 800
self.setMinimumSize(self.window_width, self.window_height)
self.label = QLabel(self)
self.label.setText('Hello')
self.label.move(150, 150)
QShortcut(QKeySequence(Qt.Key_Left), self, activated=self.move_left)
QShortcut(QKeySequence(Qt.Key_Right), self, activated=self.move_right)
QShortcut(QKeySequence(Qt.Key_Up), self, activated=self.move_up)
QShortcut(QKeySequence(Qt.Key_Down), self, activated=self.move_down)
def move_left(self):
self.label.move(self.label.pos() + QPoint(-15, 0))
def move_right(self):
self.label.move(self.label.pos() + QPoint(15, 0))
def move_up(self):
self.label.move(self.label.pos() + QPoint(0, -15))
def move_down(self):
self.label.move(self.label.pos() + QPoint(0, 15))
if __name__ == '__main__':
# don't auto scale when drag app to a different monitor.
# QApplication.setAttribute(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
app = QApplication(sys.argv)
app.setStyleSheet('''
QWidget {
font-size: 45px;
}
''')
myApp = MyApp()
myApp.show()
try:
sys.exit(app.exec_())
except SystemExit:
print('Closing Window...')
