這篇文章主要為大家詳細介紹了PyQt5每天必學之關閉窗口,具有一定的參考價值,感興趣的小伙伴們可以參考一下
最簡單的關閉一個窗口的方法是點擊標題列上的x 標誌。但是,在下面的範例中,我們將展示如何透過程式設計來控制關閉視窗。我們將使用PyQt5的訊號/槽。
以下是我們在範例中使用到的 QPushButton 控制項的建構方法。
QPushButton(string text, QWidget parent = None)
text 參數是在按鈕上顯示的文字。 parent 要將按鈕控制項放在哪裡。在下面的範例中我們要將按鈕控制項放到 QWidget裡。一個應用程式的視窗控件可以形成一個分層結構,在這個層次,大多數控件有他們的父控件,沒有父控件的控件就是頂層視窗。
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ PyQt5 教程 这个程序创建一个退出按钮。当我们按下按钮,应用程序将终止。 作者:我的世界你曾经来过 博客:http://blog.csdn.net/weiaitaowang 最后编辑:2016年7月29日 """ import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton from PyQt5.QtCore import QCoreApplication class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): btn = QPushButton('退出', self) btn.clicked.connect(QCoreApplication.instance().quit) btn.resize(btn.sizeHint()) btn.move(50, 50) self.setGeometry(300, 300, 300, 220) self.setWindowTitle('退出按钮') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
在這個範例中,我們建立了一個退出按鈕。當點擊按鈕,應用程式終止。
from PyQt5.QtCore import QCoreApplication
我們需要使用QtCore 模組中的QCoreApplication 物件
btn = QPushButton('退出', self)
#我們建立了一個按鈕,按鈕是QPushButton 類別的一個實例。建構函數的第一個參數是按鈕的標籤。第二個參數是父視窗控制項。父視窗控制項是Example 控件,這是一個繼承自 QWidget 的類別。
btn.clicked.connect(QCoreApplication.instance().quit)
此事件處理系統建立在 PyQt5 的訊號/槽的機制上。如果我們點擊該按鈕,按鈕將會發出信號,點擊訊號連接到 quit() 方法使應用程式終止。
槽可以是 Qt 的槽也可以是 Python 的任何呼叫。 QCoreApplication 包含主事件循環;它處理和調度所有事件。 instance()方法為我們提供了其當前實例。注意,區分 QCoreApplication 與 QApplication。
發送器和接收器:在通訊的兩個物件之間進行。發送器是按鈕,接收器是應用物件。
概念有些混亂,這裡整理一下:
#按鈕(btn)是發送器。點選(clicked)按鈕後,發出點擊訊號。點選訊號連接(connect)到槽(可以是 Qt 的槽也可以是 Python 的任何呼叫)。
在我們的例子中是Qt的槽,QCoreApplication處理和調度所有Qt事件,調度出instance(這個實例(接收器))的 quit 事件。
大概就這個意思了,慢慢理解吧!
程式執行後,點選退出按鈕,將關閉程式。
相關推薦:
以上是PyQt5每天必學之關閉窗口的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

toAccesselementsInapythonlist,useIndIndexing,負索引,切片,口頭化。 1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具