Python?PyQt拖動(dòng)控件對(duì)齊到網(wǎng)格的方法步驟
實(shí)現(xiàn)如下需求:
在PyQt界面上有一個(gè)控件,實(shí)現(xiàn)其可任意拖動(dòng),且鼠標(biāo)釋放時(shí)自動(dòng)對(duì)齊到網(wǎng)格。
1.控件任意拖動(dòng)并對(duì)齊到網(wǎng)格
如下按鈕(尺寸100×100),可任意拖動(dòng),釋放時(shí)對(duì)齊到網(wǎng)格(網(wǎng)格尺寸100×100)


首先給出代碼
from PyQt5.QtWidgets import QPushButton, QMainWindow, QApplication
class Button(QPushButton):
def __init__(self, parent=None):
super().__init__(parent)
self.resize(100, 100)
self.pos1 = 0 # 用于拖動(dòng)時(shí)的鼠標(biāo)位置初始值
def mousePressEvent(self, QMouseEvent):
self.pos1 = QMouseEvent.screenPos()
def mouseReleaseEvent(self, QMouseEvent) -> None:
fx, fy = self.frameGeometry().x(), self.frameGeometry().y() # 相對(duì)父控件坐標(biāo)
tx_index, ty_index = fx // 100 if fx > 99 else 0, fy // 100 if fy > 99 else 0
# 移動(dòng)到網(wǎng)格上
self.mymove(tx_index, ty_index)
def mouseMoveEvent(self, QMouseEvent):
pos2 = QMouseEvent.screenPos()
tx = int(self.frameGeometry().x() + pos2.x() - self.pos1.x())
ty = int(self.frameGeometry().y() + pos2.y() - self.pos1.y())
self.move(tx, ty)
self.pos1 = pos2
def mymove(self, tx_index, ty_index):
self.move(tx_index * 100, ty_index * 100)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('按鈕測(cè)試')
self.resize(500, 500)
self.btn = Button(self)
self.btn.setText('ABCD')
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
這里自定義Button類繼承QPushButton類,因?yàn)槲覀冃枰貙懯髽?biāo)移動(dòng)的方法來(lái)實(shí)現(xiàn)所需功能。
要實(shí)現(xiàn)任意拖動(dòng),按鈕必須跟隨鼠標(biāo),于是我們重寫mousePressEvent方法和mouseMoveEvent方法。
- 當(dāng)我們按下鼠標(biāo)時(shí),觸發(fā)mousePress事件,記錄此刻光標(biāo)位置;
- 當(dāng)光標(biāo)拖動(dòng)時(shí)觸發(fā)mouseMove事件,獲取當(dāng)前光標(biāo)位置,計(jì)算與之前位置的x和y的差值,然后加到按鈕的相對(duì)坐標(biāo)上,獲得按鈕需要移動(dòng)到的位置坐標(biāo);
- 調(diào)用move方法移動(dòng)按鈕;
- 更新pos1即按鈕位置;
- 只要光標(biāo)移動(dòng),就會(huì)觸發(fā)mouseMove事件,就會(huì)不斷移動(dòng)按鈕與更新按鈕位置,在視覺(jué)上按鈕就是在跟著光標(biāo)任意拖動(dòng)。
要實(shí)現(xiàn)鼠標(biāo)釋放時(shí)對(duì)齊到網(wǎng)格,需要重寫mouseReleaseEvent方法,用來(lái)響應(yīng)鼠標(biāo)釋放動(dòng)作。
- 當(dāng)鼠標(biāo)釋放時(shí),立即讀取按鈕的當(dāng)前相對(duì)坐標(biāo);
- 將按鈕的坐標(biāo)除以100用來(lái)獲取其在網(wǎng)格上的位置,如果坐標(biāo)小于0應(yīng)令其等于0,同時(shí)0-99的坐標(biāo)除以100也等于0,于是只要小于99就等于0;
- 然后調(diào)用自定義的mymove方法,在這個(gè)方法中,將網(wǎng)格位置換算到相對(duì)坐標(biāo),再調(diào)用move方法使其移動(dòng)到網(wǎng)格。
2.進(jìn)階:雙擊控件使其移動(dòng)到其他網(wǎng)格
移動(dòng)并對(duì)齊到網(wǎng)格的功能已經(jīng)在上一部分實(shí)現(xiàn)了,這里需要實(shí)現(xiàn)鼠標(biāo)雙擊動(dòng)作,首先給出代碼
from PyQt5.QtWidgets import QPushButton, QMainWindow, QApplication
from PyQt5.QtCore import Qt
class Button(QPushButton):
def __init__(self, parent=None):
super().__init__(parent)
self.resize(100, 100)
self.pos1 = 0 # 用于拖動(dòng)時(shí)的鼠標(biāo)位置初始值
self.x_index, self.y_index = 0, 0 # 記錄按鈕在網(wǎng)格上的位置
def mousePressEvent(self, QMouseEvent):
if QMouseEvent.buttons() == Qt.LeftButton:
print('左鍵按下')
self.pos1 = QMouseEvent.screenPos()
elif QMouseEvent.buttons() == Qt.RightButton:
print('右鍵按下')
self.pos1 = QMouseEvent.screenPos()
def mouseReleaseEvent(self, QMouseEvent) -> None:
print('鼠標(biāo)釋放')
fx, fy = self.frameGeometry().x(), self.frameGeometry().y() # 相對(duì)父控件坐標(biāo)
tx_index, ty_index = fx // 100 if fx > 99 else 0, fy // 100 if fy > 99 else 0
# 移動(dòng)到網(wǎng)格上
self.x_index, self.y_index = tx_index, ty_index
self.mymove(tx_index, ty_index)
def mouseDoubleClickEvent(self, QMouseEvent):
if QMouseEvent.buttons() == Qt.LeftButton:
print('左鍵雙擊')
self.x_index += 1
self.y_index += 1
self.mymove(self.x_index, self.y_index)
elif QMouseEvent.buttons() == Qt.RightButton:
print('右鍵雙擊')
def mouseMoveEvent(self, QMouseEvent):
if QMouseEvent.buttons() == Qt.LeftButton:
pos2 = QMouseEvent.screenPos()
tx = int(self.frameGeometry().x() + pos2.x() - self.pos1.x())
ty = int(self.frameGeometry().y() + pos2.y() - self.pos1.y())
self.move(tx, ty)
self.pos1 = pos2
def mymove(self, tx_index, ty_index):
self.move(tx_index * 100, ty_index * 100)
print(f'按鈕移動(dòng)到({tx_index}, {ty_index})')
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('按鈕測(cè)試')
self.resize(500, 500)
self.btn = Button(self)
self.btn.setText('ABCD')
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())在這里多了一些實(shí)例屬性,如self.x_index, self.y_index用來(lái)記錄按鈕在網(wǎng)格上的位置。
要實(shí)現(xiàn)雙擊動(dòng)作,必須重寫mouseDoubleClickEvent方法,在mouseDoubleClickEvent方法中,我們首先將self.x_index, self.y_index進(jìn)行修改,以更改按鈕要移動(dòng)到的位置,然后調(diào)用mymove方法進(jìn)行移動(dòng)。
此外,代碼還進(jìn)行了左鍵與右鍵的判斷,當(dāng)左鍵進(jìn)行操作時(shí),按鈕可以更改位置,右鍵操作時(shí)不可更改。
總結(jié)
到此這篇關(guān)于Python PyQt拖動(dòng)控件對(duì)齊到網(wǎng)格的文章就介紹到這了,更多相關(guān)PyQt拖動(dòng)控件對(duì)齊到網(wǎng)格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python將xml和xsl轉(zhuǎn)換為html的方法
這篇文章主要介紹了Python將xml和xsl轉(zhuǎn)換為html的方法,實(shí)例分析了使用libxml2模塊操作xml和xsl轉(zhuǎn)換為html的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
python實(shí)現(xiàn)圖片數(shù)據(jù)增強(qiáng)的示例詳解
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖片數(shù)據(jù)增強(qiáng)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2023-10-10
python實(shí)現(xiàn)合并兩個(gè)排序的鏈表
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)合并兩個(gè)排序的鏈表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
python list等分并從等分的子集中隨機(jī)選取一個(gè)數(shù)
這篇文章主要介紹了python list等分并從等分的子集中隨機(jī)選取一個(gè)數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
使用python進(jìn)行PostgreSQL數(shù)據(jù)庫(kù)連接全過(guò)程
這篇文章主要介紹了使用python進(jìn)行PostgreSQL數(shù)據(jù)庫(kù)連接的相關(guān)資料,包括安裝psycopg2模塊、使用PyCharm進(jìn)行圖形化連接、代碼連接數(shù)據(jù)庫(kù)的方法、以及如何執(zhí)行DML和DQL操作,需要的朋友可以參考下2025-03-03
Python數(shù)組遍歷的簡(jiǎn)單實(shí)現(xiàn)方法小結(jié)
這篇文章主要介紹了Python數(shù)組遍歷的簡(jiǎn)單實(shí)現(xiàn)方法,結(jié)合實(shí)例總結(jié)分析了Python針對(duì)數(shù)組的元素,索引常用遍歷技巧,需要的朋友可以參考下2016-04-04
Python中的logging模塊實(shí)現(xiàn)日志打印
這篇文章主要介紹了Python中的logging模塊實(shí)現(xiàn)日志打印,其實(shí)不止print打印日志方便排查問(wèn)題,Python自帶的logging模塊,也可以很簡(jiǎn)單就能實(shí)現(xiàn)日志的配置和打印,下面來(lái)看看具體的實(shí)現(xiàn)過(guò)程吧,需要的朋友可以參考一下2022-03-03

