PyQt5 - 使用箭头键在窗口内移动标签位置
在本文中,我们将了解如何使用箭头键在窗口内移动标签,即当箭头键(方向键)被按下时,它会朝那个方向移动。例如,当用户按下向上箭头键时,标签将类似地向上移动,其他箭头键标签也会改变其位置。
概念:我们可以通过增加/减少标签的坐标值来改变标签的位置,但如果它到达边的任何一端,则不要增加或减少下面是每个箭头键按下时给出的数据.
When Up arrow key is pressed : X Co-ordinate remain same, decrement the Y Co-ordinate
When Down arrow key is pressed : X Co-ordinate remain same, increment the Y Co-ordinate
When Left arrow key is pressed : Decrement X Co-ordinate, Y Co-ordinate remain same
When Right arrow key is pressed : Increment X Co-ordinate, Y Co-ordinate remain same
下面是边缘限制数据,因此标签应保留在窗口中
For top edge the y- co-ordinate should be always greater than 0
For bottom edge the y- co-ordinate should be always less than height of window – height of label
For left edge the x- co-ordinate should be always greater than 0
For right edge the x- co-ordinate should be always less than width of window – width of label
Implementation Steps :
1. Create a Main window
2. Create a label inside the main window
3. Add style sheet geometry to the label
4. Create a speed variable
5. Override the key press event
6. Inside the key press event get the current x and y co-ordinates of the label
7. And check which key key is pressed and check if side end is not reached then update the x and y co-ordinates with the help of move
method by incrementing/decrementing speed value from them
下面是实现
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
# setting title
self.setWindowTitle("Python ")
# width of window
self.w_width = 500
# height of window
self.w_height = 500
# setting geometry
self.setGeometry(100, 100, self.w_width, self.w_height)
# calling method
self.UiComponents()
# showing all the widgets
self.show()
# speed variable
self.speed = 15
# method for components
def UiComponents(self):
# creating a label
self.label = QLabel(self)
# label width
self.l_width = 40
# label height
self.l_height = 40
# setting geometry to the label
self.label.setGeometry(200, 200, self.l_width, self.l_height)
# setting stylesheet to the label
self.label.setStyleSheet("QLabel"
"{"
"border : 4px solid darkgreen;"
"background : lightgreen;"
"}")
# override the key press event
def keyPressEvent(self, event):
# get the current co-ordinates of the label
# X Co-ordinate
x = self.label.x()
# Y Co-ordinate
y = self.label.y()
# if up arrow key is pressed
if event.key() == Qt.Key_Up:
# if top position is attained
if y > 0:
self.label.move(x, y - self.speed)
# if down arrow key is pressed
elif event.key() == Qt.Key_Down:
# if bottom position is attained
# for bottom point, bottom co-ordinate will be
# height of window - height of label
if y < self.w_height - self.l_height:
self.label.move(x, y + self.speed)
# if left arrow key is pressed
elif event.key() == Qt.Key_Left:
# if left end position is attained
if x > 0:
self.label.move(x - self.speed, y)
# if down arrow key is pressed
elif event.key() == Qt.Key_Right:
# if right end position is attained
# for right end point, right co-ordinate will be
# width of window - width of label
if x < self.w_width - self.l_width:
self.label.move(x + self.speed, y)
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :