亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python GUI庫圖形界面開發(fā)之PyQt5拖放控件實(shí)例詳解

 更新時間:2020年02月25日 10:22:45   作者:州的先生  
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5使用拖放控件實(shí)例詳解,需要的朋友可以參考下

本篇,我們學(xué)習(xí)PyQt5界面中拖放(Drag 和Drop)控件。

拖放動作

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

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

在PyQt中實(shí)現(xiàn)拖放

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

使用Qt設(shè)計(jì)師或者使用API都可以實(shí)現(xiàn)。我們先使用Qt設(shè)計(jì)師將GUI的圖形設(shè)計(jì)出來,在之前的GUI的基礎(chǔ)上,我們新建一個選項(xiàng)卡。

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

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

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

我們先將UI文件保存并轉(zhuǎn)換為Python文件。

pyuic5 -o conplex_window_drag.py conplex_window.ui

然后,新建一個Python文嘉drag.py,在文件中引入剛剛轉(zhuǎn)換好的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_())

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

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

  # 設(shè)置LCD數(shù)字
  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)

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

  # 停止進(jìn)度欄
  def stop_progressbar(self):
    self.stop_progress = True
    try:
      self.run_thread.stop()
    except:
      pass
  # 重設(shè)進(jìn)度欄
  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)
      # 發(fā)出一個新值的信號
      self.counter_value.emit(self.counter)

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

if __name__ == "__main__":
  MainWindow()

運(yùn)行代碼正常:

接著,我們創(chuàng)建一個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屬性設(shè)置按鈕接收拖放事件,創(chuàng)建一個dragEnterEvent()方法用來設(shè)置拖的事件響應(yīng),創(chuàng)建一個dropEvent()方法用來設(shè)置放的事件響應(yīng)。

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

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)
    ……

最后,運(yùn)行一下看看:

在上面的程序中,我們能夠?qū)⑽谋就戏诺桨粹o上。

好了python GUI庫圖形界面開發(fā)中PyQt5拖放控件的實(shí)例就是這些,更多關(guān)于python PyQt5 GUI庫圖形界面開發(fā)請查看下面的相關(guān)鏈接

相關(guān)文章

  • Python3中PyQt5簡單實(shí)現(xiàn)文件打開及保存

    Python3中PyQt5簡單實(shí)現(xiàn)文件打開及保存

    本文將結(jié)合實(shí)例代碼,介紹Python3中PyQt5簡單實(shí)現(xiàn)文件打開及保存,具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式

    Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式

    今天小編就為大家分享一篇Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • python實(shí)現(xiàn)對svn操作及信息獲取

    python實(shí)現(xiàn)對svn操作及信息獲取

    這篇文章主要介紹了python實(shí)現(xiàn)對svn的操作及信息獲取示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-10-10
  • Python實(shí)現(xiàn)的調(diào)用C語言函數(shù)功能簡單實(shí)例

    Python實(shí)現(xiàn)的調(diào)用C語言函數(shù)功能簡單實(shí)例

    這篇文章主要介紹了Python實(shí)現(xiàn)的調(diào)用C語言函數(shù)功能,結(jié)合簡單實(shí)例形式分析了Python使用ctypes模塊調(diào)用C語言函數(shù)的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • Python中的取整、取余運(yùn)算方法

    Python中的取整、取余運(yùn)算方法

    數(shù)據(jù)處理是編程中不可避免的,很多時候都需要根據(jù)需求把獲取到的數(shù)據(jù)進(jìn)行處理,取整則是最基本的數(shù)據(jù)處理。取整的方式則包括向下取整、四舍五入、向上取整等等,這篇文章主要介紹了Python中的取整、取余運(yùn)算,需要的朋友可以參考下
    2022-11-11
  • 從局部變量和全局變量開始全面解析Python中變量的作用域

    從局部變量和全局變量開始全面解析Python中變量的作用域

    無論是以類為基礎(chǔ)的面相對象編程,還是單純函數(shù)內(nèi)部變量的定義,變量的作用域始終是Python學(xué)習(xí)中一個必須理解掌握的環(huán)節(jié),下面我們從局部變量和全局變量開始全面解析Python中變量的作用域,需要的朋友可以參考下
    2016-06-06
  • wxPython實(shí)現(xiàn)列表增刪改查功能

    wxPython實(shí)現(xiàn)列表增刪改查功能

    這篇文章主要為大家詳細(xì)介紹了wxPython實(shí)現(xiàn)列表增刪改查功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • 簡單的編程0基礎(chǔ)下Python入門指引

    簡單的編程0基礎(chǔ)下Python入門指引

    這篇文章主要介紹了簡單的編程0基礎(chǔ)下Python入門指引,包括從各個系統(tǒng)的Python安裝和簡單的語句知識,需要的朋友可以參考下
    2015-04-04
  • Python模塊的定義,模塊的導(dǎo)入,__name__用法實(shí)例分析

    Python模塊的定義,模塊的導(dǎo)入,__name__用法實(shí)例分析

    這篇文章主要介紹了Python模塊的定義,模塊的導(dǎo)入,__name__用法,結(jié)合實(shí)例形式分析了Python的概念、功能、導(dǎo)入及__name__相關(guān)使用技巧,需要的朋友可以參考下
    2020-01-01
  • 解決matplotlib庫show()方法不顯示圖片的問題

    解決matplotlib庫show()方法不顯示圖片的問題

    今天小編就為大家分享一篇解決matplotlib庫show()方法不顯示圖片的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05

最新評論