在這節(jié)教程中,我們將探討PyQt5中的拖放操作。
在計算機圖形用戶界面(GUI)中,拖放是在某個虛擬對象上點擊并拖動到另一個位置或虛擬對象上的操作。它通常用于調(diào)用多個動作,或為兩個抽象對象創(chuàng)建某些聯(lián)系。
拖放是圖形用戶界面的一部分。拖放可以使用戶直觀地完成某些復雜的操作。
通常我們可以對兩種事物進行拖放操作:數(shù)據(jù)或某些圖形對象。如果我們將某個應用中的圖片拖放到另一個應用,我們拖放的是二進制數(shù)據(jù)。如果將Firefox的某個標簽頁拖放到其他地方,我們拖放的是一個圖形組件。
簡單的拖放
在第一個示例中我們要創(chuàng)建一個QLineEdit和一個QPushButton,并通過將LineEdit中的文本拖放到按鈕上來改變按鈕的標簽。
import sys from PyQt5.QtWidgets import (QPushButton, QWidget, QLineEdit, QApplication) class Button(QPushButton): def __init__(self, title, parent): super().__init__(title, parent) self.setAcceptDrops(True) def dragEnterEvent(self, e): if e.mimeData().hasFormat("text/plain"): e.accept() else: e.ignore() def dropEvent(self, e): self.setText(e.mimeData().text()) class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): edit = QLineEdit("", self) edit.setDragEnabled(True) edit.move(30, 65) button = Button("Button", self) button.move(190, 65) self.setWindowTitle("Simple drag & drop") self.setGeometry(300, 300, 300, 150) self.show() if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
當前文章:PyQt5實現(xiàn)拖放功能-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://aaarwkj.com/article38/cdpcpp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、虛擬主機、網(wǎng)站維護、手機網(wǎng)站建設、標簽優(yōu)化、關鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容