Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of various commonly used dialogs in PyQt5

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

QDialog basic dialog box

The main subcategories are QMessageBox, QFileDialog, QFontDialog, QInputDialog, etc.

Show basic form

Test code

#-*-coding:utf8-*-import sysfrom PyQt5.QtCore import Qtfrom PyQt5.QtWidgets import QMainWindow, QApplicationclass MyDialog (QMainWindow): def _ _ init__ (self, parent=None): super (MyDialog, self). _ _ init__ (parent) # set window title self.setWindowTitle ("test window") # set window size self.resize (350 # set window to modeless self.setWindowModality (Qt.NonModal) # set window to window mode Self.setWindowModality (Qt.WindowModal) # sets the window to application mode when the program blocks interaction with the parent window of the dialog box when it has not finished processing the current dialog box Block interaction with any other window of a person self.setWindowModality (Qt.ApplicationModal) if _ _ name__ ='_ _ main__': app = QApplication (sys.argv) ui_demo = MyDialog () ui_demo.show () sys.exit (app.exec_ ()) QMessageBox generic pop-up dialog box

Dialog box function

# Information prompt dialog QMessageBox.information (ParentQWidght, Title, Text, Buttons, DefaultButton) # question response dialog QMessageBox.question (ParentQWidght, Title, Text, Buttons, DefaultButton) # warning dialog box QMessageBox.warning (ParentQWidght, Title, Text, Buttons, DefaultButton) # about dialog QMessageBox.about (ParentQWidght, Title, Text, Buttons, DefaultButton) # serious warning dialog box QMessageBox.critical (ParentQWidght, Title, Text, Buttons, DefaultButton)

Buttons defined in the PyQt5 source code

# calling method: QMessage.xxxAbort = 262144AcceptRole = 0ActionRole = 3Apply = 33554432ApplyRole = 8ButtonMask =-769Cancel = 4194304Close = 2097152Critical = 3Default = 256DestructiveRole = 2Discard = 8388608Escape = 512FirstButton = 1024FlagMask = 768Help = 4Ignore = 1048576Information = 1InvalidRole =-1LastButton = 134217728No = 65536NoAll = 131072NoButton = 0NoIcon = 6NoToAll = 131072Ok = 1024Open = 8192Question = 4RejectRole = 1Reset = 67108864ResetRole = 7RestoreDefaults = 134217728Retry = 524288Save = 524288Save = 2048SaveAll = 2048SaveAll = 4096Warning = 4096Warning = 4096Warning = 4096Warning = 4096Warning = 4096Warning

Test code

From PyQt5.QtWidgets import QMessageBox # only shows if (msg=='connect success'): conn.close () QMessageBox.information (self, "warm reminder", "database connection succeeded!" , QMessageBox.Yes, QMessageBox.Yes) else: QMessageBox.warning (self, "warning message", "message from the database: {0}" .format (msg), QMessageBox.Yes, QMessageBox.Yes) # handling the action after the user replied reply = QMessageBox.warning (self, "warm reminder", "exit soon, OK?" , QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if (reply== QMessageBox.Yes): QCloseEvent.accept () if (reply==QMessageBox.No): QCloseEvent.ignore () QInputDialog standard input dialog box

Commonly used value function

# get real QInputDialog.getDouble (ParentQWidght,Title,Text) # get integer QInputDialog.getInt (ParentQWidght,Title,Text) # get input QInputDialog.getItem (ParentQWidght,Title,Text,DefaultItem, isOperation) # get text QInputDialog.getText (ParentQWidght,Title,Text) # get multi-line text QInputDialog.getMultiLineText (ParentQWidght,Title,Text,DefaultText)

Test code

From PyQt5.QtWidgets import QInputDialognumber, ok = QInputDialog.getInt (self, "enter numbers dialog", "enter numbers") if (ok): print (number) number, ok = QInputDialog.getDouble (self, "enter numbers dialog", "enter numbers") if (ok): print (number) text, ok = QInputDialog.getText (self, "enter numbers dialog box", "enter numbers") if (ok): print (text) mul_text Ok = QInputDialog.getMultiLineText (self, "xixi", "", "hehe") if (ok): print (mul_text) item, ok = QInputDialog.getItem (self, ", ['One',' Two', 'Three'], 0, False) if (ok): print (item) QFontDialog font selection dialog box

Test code

From PyQt5.QtWidgets import QFontDialogfont, ok = QFontDialog.getFont () if (ok): # set font style function self.lintEdit.setFont (font) QColorDialog color selection dialog box

Test code

From PyQt5.QtGui import QPalettefrom PyQt5.QtWidgets import QColorDialogcolor = QColorDialog.getColor () if (color.isValid ()): # set color name self.label.setText (color.name ()) # set color self.label.setPalette (QPalette (color)) # set auto-fill background color self.label.setAutoFillBackground (True)

QFileDialog file selection dialog box

Common function

# get the open path of the file QFileDialog.getOpenFileName () # get the open path of a group of files QFileDialog.getOpenFileNames () # get the save path of the file QFileDialog.getSaveFileName () # set the file type QFileDialog.setFileMode () "" enumeration constant includes: QFileDialog.AnyFile QFileDialog.ExistingFile QFileDialog.Directory QFileDialog.ExistingFiles "" # set the file filter The displayed file type QFileDialog.setFilter () # gets the directory QFileDialog.getExistingDirectory () that already exists

Test code

File_name, ok = QFileDialog.getOpenFileName (self, "Open files",'/', "Images files (* .jpg * .gif)") if (ok): print (file_name) files_name, ok = QFileDialog.getOpenFileNames (self, "Open a set of files",'/', "All Files (*)" Text Files (* .txt) ") if (ok): for i in files_name: print (I) file_name, ok = QFileDialog.getSaveFileName (self," Save File ",'/'," Images files (* .jpg * .gif) ") if (ok): print (files_name) QFileDialog.setFileMode (QFileDialog.AnyFile) QFileDialog.setFilter (" Images files (* .jpg * .gif) ") path = QFileDialog.getExistingDirectory (self," Select Directory ") /') print (path)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report