彈窗大全
阿新 • • 發佈:2019-01-05
from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt, pyqtSignal, QTimer class MyWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle('PyQt5 彈出視窗(框)大全') self.resize(400, 300) # 全域性佈局(注意引數 self) wl = QVBoxLayout(self)# 區域性佈局 h1 = QHBoxLayout() # 輸入框 h2 = QHBoxLayout() # 訊息視窗 h3 = QHBoxLayout() # 檔案(夾)開啟,儲存 h4 = QHBoxLayout() # 顏色、字型、自定義 btn11 = QPushButton('輸入:整數') btn12 = QPushButton('輸入:小數') btn13 = QPushButton('輸入:文字') btn14 = QPushButton('輸入:多文') btn15 = QPushButton('輸入:選項') btn21 = QPushButton('訊息:資訊') btn22 = QPushButton('訊息:問答') btn23 = QPushButton('訊息:警告') btn24 = QPushButton('訊息:危險') btn25 = QPushButton('訊息:關於') btn31 = QPushButton('檔案:資料夾') btn32= QPushButton('檔案:單檔案') btn33 = QPushButton('檔案:多檔案') btn34 = QPushButton('檔案:儲存') btn35 = QPushButton('檔案:另存為') btn41 = QPushButton('顏色') btn42 = QPushButton('字型') btn43 = QPushButton('自定義') for btn in (btn11, btn12, btn13, btn14, btn15): h1.addWidget(btn) for btn in (btn21, btn22, btn23, btn24, btn25): h2.addWidget(btn) for btn in (btn31, btn32, btn33, btn34, btn35): h3.addWidget(btn) for btn in (btn41, btn42, btn43): h4.addWidget(btn) btn11.clicked.connect(self.do_btn11) # 輸入:整數 btn12.clicked.connect(self.do_btn12) # 輸入:小數 btn13.clicked.connect(self.do_btn13) # 輸入:文字 btn14.clicked.connect(self.do_btn14) # 輸入:多文 btn15.clicked.connect(self.do_btn15) # 輸入:選項 btn21.clicked.connect(self.do_btn21) # 訊息:資訊 btn22.clicked.connect(self.do_btn22) # 訊息:問答 btn23.clicked.connect(self.do_btn23) # 訊息:警告 btn24.clicked.connect(self.do_btn24) # 訊息:危險 btn25.clicked.connect(self.do_btn25) # 訊息:關於 btn31.clicked.connect(self.do_btn31) # 檔案:資料夾 btn32.clicked.connect(self.do_btn32) # 檔案:單檔案 btn33.clicked.connect(self.do_btn33) # 檔案:多檔案 btn34.clicked.connect(self.do_btn34) # 檔案:儲存 btn35.clicked.connect(self.do_btn35) # 檔案:另存為 btn41.clicked.connect(self.do_btn41) # 顏色 btn42.clicked.connect(self.do_btn42) # 字型 btn43.clicked.connect(self.do_btn43) # 自定義 # 加到全域性佈局 wl.addLayout(h1) wl.addLayout(h2) wl.addLayout(h3) wl.addLayout(h4) self.window2 = MyWindow2() # 自定義視窗 self.window2.before_close_signal.connect(self.echo) # 接收自定義視窗關閉時傳送過來的訊號,交給 echo 函式顯示 def echo(self, value): '''顯示對話方塊返回值''' QMessageBox.information(self, "返回值", "得到:{}\n\ntype: {}".format(value, type(value)), QMessageBox.Yes | QMessageBox.No) #pass # ===================================================================== def do_btn11(self, event): # 輸入:整數 #後面四個數字的作用依次是 初始值 最小值 最大值 步幅 value, ok = QInputDialog.getInt(self, "輸入框標題", "這是提示資訊\n\n請輸入整數:", 37, -10000, 10000, 2) #self.echo(value) def do_btn12(self, event): # 輸入:小數 #後面四個數字的作用依次是 初始值 最小值 最大值 小數點後位數 value, ok = QInputDialog.getDouble(self, "輸入框標題", "這是提示資訊\n\n請輸入整數:", 37.56, -10000, 10000, 2) self.echo(value) def do_btn13(self, event): # 輸入:文字 #第三個引數表示顯示型別,可選,有正常(QLineEdit.Normal)、密碼( QLineEdit. Password)、不顯示( QLineEdit. NoEcho)三種情況 value, ok = QInputDialog.getText(self, "輸入框標題", "這是提示資訊\n\n請輸入文字:", QLineEdit.Normal, "這是預設值") self.echo(value) def do_btn14(self, event): # 輸入:多文 value, ok = QInputDialog.getMultiLineText(self, "輸入框標題", "這是提示資訊\n\n請輸入地址:", "預設的\n我的地址是\n中國廣東廣州番禺") self.echo(value) def do_btn15(self, event): # 輸入:選項 #1為預設選中選專案,True/False 列表框是否可編輯。 items = ["Spring", "Summer", "Fall", "Winter"] value, ok = QInputDialog.getItem(self, "輸入框標題", "這是提示資訊\n\n請選擇季節:", items, 1, True) self.echo(value) # ===================================================================== def do_btn21(self, event): # 訊息:資訊 reply = QMessageBox.information(self, "訊息框標題", "這是一條訊息。", QMessageBox.Yes | QMessageBox.No) self.echo(reply) def do_btn22(self, event): # 訊息:問答 reply = QMessageBox.question(self, "訊息框標題", "這是一條問答嗎?", QMessageBox.Yes | QMessageBox.No) self.echo(reply) def do_btn23(self, event): # 訊息:警告 reply = QMessageBox.warning(self, "訊息框標題", "這是一條警告!", QMessageBox.Yes | QMessageBox.No) self.echo(reply) def do_btn24(self, event): # 訊息:危險 reply = QMessageBox.ctitical(self, "訊息框標題", "危險!程式即將強制退出!!!\n\n這個按鈕再也點不開。", QMessageBox.Yes | QMessageBox.No) self.echo(reply) def do_btn25(self, event): # 訊息:關於 reply = QMessageBox.about(self, "訊息框標題", "這是關於軟體的說明。。。", QMessageBox.Yes | QMessageBox.No) self.echo(reply) # ===================================================================== def do_btn31(self, event): # 檔案:資料夾 dir = QFileDialog.getExistingDirectory(self, "選取資料夾", "C:/") # 起始路徑 self.echo(dir) def do_btn32(self, event): # 檔案:單檔案 file_, filetype = QFileDialog.getOpenFileName(self, "選取檔案", "C:/", "All Files (*);;Text Files (*.txt)") #設定副檔名過濾,注意用雙分號間隔 self.echo(file_) def do_btn33(self, event): # 檔案:多檔案 files, ok = QFileDialog.getOpenFileNames(self, "多檔案選擇", "C:/", "All Files (*);;Text Files (*.txt)") self.echo(files) def do_btn34(self, event): # 檔案:儲存 file_, ok = QFileDialog.getSaveFileName(self, "檔案儲存", "C:/", "All Files (*);;Text Files (*.txt)") self.echo(file_) def do_btn35(self, event): # 檔案:另存為 file_, ok = QFileDialog.getSaveFileName(self, "檔案另存為", "C:/", "All Files (*);;Text Files (*.txt)") self.echo(file_) # ===================================================================== def do_btn41(self, event): # 顏色 color = QColorDialog.getColor(Qt.blue, self, "Select Color") self.echo(color) def do_btn42(self, event): # 字型 font, ok = QFontDialog.getFont() self.echo(font) def do_btn43(self, event): # 自定義 self.window2.show() class MyWindow2(QWidget): '''自定義視窗''' # 知識點: # 1.為了得到返回值用到了自定義的訊號/槽 # 2.為了顯示動態數字,使用了計時器 before_close_signal = pyqtSignal(int) # 自定義訊號(int型別) def __init__(self): super().__init__() self.sec = 0 self.setWindowTitle('自定義視窗') self.resize(200,150) self.lcd = QLCDNumber(18, self) btn1 = QPushButton(self, text="測試") btn2 = QPushButton(self, text="關閉") layout = QVBoxLayout(self) layout.addWidget(self.lcd) layout.addWidget(btn1) layout.addWidget(btn2) self.timer = QTimer() self.timer.timeout.connect(self.update) # 每次計時結束,觸發update btn1.clicked.connect(self.startTimer) btn2.clicked.connect(self.stopTimer) # 去到關閉前的處理 def update(self): self.sec += 1 self.lcd.display(self.sec) # LED顯示數字+1 def startTimer(self): self.timer.start(1000) # 計時器每秒計數 def stopTimer(self): self.timer.stop() self.sec = 0 self.before_close_signal.emit(self.lcd.value()) # 傳送訊號,帶引數 888 self.close() # 然後視窗關閉 # 預設關閉事件 def closeEvent(self, e): self.stopTimer() if __name__=="__main__": import sys app = QApplication(sys.argv) win = MyWindow() win.show() sys.exit(app.exec_())