溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python中GUI庫圖形界面開發之PyQt5拖放控件的示例分析

發布時間:2021-06-10 09:38:34 來源:億速云 閱讀:403 作者:小新 欄目:開發技術

小編給大家分享一下python中GUI庫圖形界面開發之PyQt5拖放控件的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

拖放動作

在GUI中,拖放指的是點擊一個對象,并將其拖動到另一個對象上的動作。比如百度云PC客戶端支持的拖放文件以快速移動文件:

python中GUI庫圖形界面開發之PyQt5拖放控件的示例分析

拖放動作能夠很直觀很方便的在GUI程序中完成一些很復雜或繁瑣的操作。

在PyQt中實現拖放

在PyQt5中,我們也可以很輕松地使用拖放功能。

使用Qt設計師或者使用API都可以實現。我們先使用Qt設計師將GUI的圖形設計出來,在之前的GUI的基礎上,我們新建一個選項卡。

python中GUI庫圖形界面開發之PyQt5拖放控件的示例分析

我們新建了一個選項卡,然后在里面放置了一個LineEdit部件,一個PushButton部件,兩個ListWidget部件。

對于簡單的拖放效果,我們可以直接使用Qt設計師中的選項進行設置。例如,我們直接可以使用dragEnable屬性、dragDropOverwriteMode屬性、dragDropMode屬性為ListWidget部件設置拖放功能:

python中GUI庫圖形界面開發之PyQt5拖放控件的示例分析

而一些稍微復雜的拖放功能,就需要編寫Python邏輯處理代碼來完成了。

我們先將UI文件保存并轉換為Python文件。

pyuic5 -o conplex_window_drag.py conplex_window.ui

然后,新建一個Python文嘉drag.py,在文件中引入剛剛轉換好的Python文件:

# coding:utf-8
# 州的先生 zmister.com Python GUI教程

from PyQt5 import QtCore,QtWidgets,QtGui
from GUI import conplex_window_drag
import sys
import time

class MainWindow(object):
  def __init__(self):
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    self.ui = conplex_window_drag.Ui_MainWindow()
    self.ui.setupUi(MainWindow)

    self.update_date()
    self.update_calendar()

    self.set_lcd()
    self.set_dial()

    self.update_progressbar()

    self.set_font()
    MainWindow.show()
    sys.exit(app.exec_())

  # 修改日期修改器數值
  def update_date(self):
    self.ui.dateEdit.setDate(self.ui.calendarWidget.selectedDate())

  # 日歷信號槽
  def update_calendar(self):
    self.ui.calendarWidget.selectionChanged.connect(self.update_date)

  # 設置LCD數字
  def set_lcd(self):
    self.ui.lcdNumber.display(self.ui.dial.value())

  # 刻度盤信號槽
  def set_dial(self):
    self.ui.dial.valueChanged['int'].connect(self.set_lcd)

  # 州的先生 zmister.com
  # 按鈕信號槽
  def update_progressbar(self):
    self.ui.radioButton.clicked.connect(self.start_progressbar)
    self.ui.radioButton_2.clicked.connect(self.stop_progressbar)
    self.ui.radioButton_3.clicked.connect(self.reset_progressbar)
    self.progress_value = 0
    self.stop_progress = False

  def progressbar_counter(self, start_value=0):
    self.run_thread = RunThread(parent=None, counter_start=start_value)
    self.run_thread.start()
    self.run_thread.counter_value.connect(self.set_progressbar)

  def set_progressbar(self, counter):
    if not self.stop_progress:
      self.ui.progressBar.setValue(counter)

  # 啟動進度欄
  def start_progressbar(self):
    self.stop_progress = False
    self.progress_value = self.ui.progressBar.value()
    self.progressbar_counter(self.progress_value)

  # 停止進度欄
  def stop_progressbar(self):
    self.stop_progress = True
    try:
      self.run_thread.stop()
    except:
      pass
  # 重設進度欄
  def reset_progressbar(self):
    self.stop_progressbar()
    self.progress_value = 0
    self.ui.progressBar.reset()
    self.stop_progress = False

  # 字體選擇
  def set_font(self):
    self.ui.fontComboBox.activated['QString'].connect(self.ui.label.setText)

class RunThread(QtCore.QThread):
  # 定義一個新的信號
  counter_value = QtCore.pyqtSignal(int)

  def __init__(self, parent=None, counter_start=0):
    super(RunThread, self).__init__(parent)
    self.counter = counter_start
    self.is_running = True

  def run(self):
    while self.counter < 100 and self.is_running == True:
      time.sleep(0.1)
      self.counter += 1
      print(self.counter)
      # 發出一個新值的信號
      self.counter_value.emit(self.counter)

  def stop(self):
    self.is_running = False
    print('線程停止中...')
    self.terminate()

if __name__ == "__main__":
  MainWindow()

運行代碼正常:

python中GUI庫圖形界面開發之PyQt5拖放控件的示例分析

接著,我們創建一個DragDropButton()類,用來處理按鈕的拖放:

class DragDropButton(QtWidgets.QPushButton):
  
  def __init__(self, text, parent):
    super().__init__(text, parent)    
    self.setAcceptDrops(True)
    
  def dragEnterEvent(self, event):
    if event.mimeData().hasFormat('text/plain'):
      event.accept()
    else:
      event.ignore()
      
  def dropEvent(self, event):
    self.setText(event.mimeData().text())

我們使用setAcceptDrops屬性設置按鈕接收拖放事件,創建一個dragEnterEvent()方法用來設置拖的事件響應,創建一個dropEvent()方法用來設置放的事件響應。

接著我們在MainWindow()主類中,調用它:

class MainWindow(object):
  def __init__(self):
    ……
    self.ui.pushButton.hide()
    self.pushButton = DragDropButton("拖放按鈕",MainWindow)
    self.ui.gridLayout_5.addWidget(self.pushButton,0, 1, 1, 2)
    ……

最后,運行一下看看:

python中GUI庫圖形界面開發之PyQt5拖放控件的示例分析

在上面的程序中,我們能夠將文本拖放到按鈕上。

看完了這篇文章,相信你對“python中GUI庫圖形界面開發之PyQt5拖放控件的示例分析”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女