The Qmainwindow main window provides the user with an application framework that has its own layout to add controls to the layout. For example, add toolbars, menu bars, and status bars to the layout manager.
Window Type Introduction
The Qmainwindow, Qwidget, and qdialog three classes are used to create a window.
The Qmainwindow window can contain menu bars, toolbars, status bars, title bars, and so on, which are the most common forms of Windows.
Qwidget is the base class for the dialog window. Dialogs are primarily used to perform short-term tasks or interact with the user, either modal or non-modal.
If it is the main window, use the Qmainwindow class, and if it is a dialog box, use the Qdialog class, or if you are not sure, or possibly as a top-level window, you might embed it in another window, using the Qwidget class.
Creating the main window
The more important methods in the Qmainwindow class are shown in the following table:
| Method |
Description |
| Addtoolbar () |
Add toolbar |
| Centralwidget () |
Returns a control in the center of the window that returns NULL when not set |
| MenuBar () |
Return to the menu bar of the main window |
| Setcentralwidget () |
To set a control in the center of a window |
| Setsstatusbar () |
Set the status bar |
| StatusBar () |
After you get the status bar object, call the ShowMessage (Message,int timeout=0) method of the status bar object to display the status bar information |
The code on the direct petition
code example creating the main window
#-*-Coding:utf-8-*-"Introduction" PyQT5 main Window Example "Import SysFrom Pyqt5.qtwidgetsImport Qmainwindow, QapplicationFrom Pyqt5.qtguiImport Qiconclass mainwidget (QMainWindow): Span class= "hljs-function" >def __init__ (Self,parent=none): Super (Mainwidget,self). __init__ (parent) # set main form label Self.setwindowtitle ( "qmainwindow example") Self.resize ( Span class= "Hljs-number" >400, 200) Self.status = Self.statusbar (www.boshenyl.cn ) Self.status.showMessage ( "This is the status bar hint", 5000) if __name__ = "__main__": App = Qapplication (SYS.ARGV) App.setwindowicon (Qicon ( "Www.yszx11.cn/ /images/cartoon1.ico")) main = Mainwidget ( www.mhylpt.com/) Main.show () Sys.exit (App.exec_ ())
Move and close the main window
#coding = ' Utf-8 '#将窗口移动到屏幕中间和关闭主窗口的例子Import SysFrom Pyqt5.qtwidgetsImport Qdesktopwidget,qmainwindow,qhboxlayout,qpushbutton,qapplication,qwidgetClassWinForm(Qmainwindow):Def__init__(Self,parent=none): Super (WinForm, self). __init__ (parent) Self.setwindowtitle (' Window placed in the middle and close the main window example ') self.resize (370,) Self.center (www.wanmeiyuele.cn) Self.button1 = Qpushbutton (' Close main window ')#将按钮的clicked信号与onButtonClick槽函数关联起来 Self.button1.clicked.connect (self.onbuttonclick) layout = Qhboxlayout () Layout.addwidget (self.button1) main_frame = Qwidget () main_frame.setlayout (layout) Self.setcentralwidget (main_frame )Defcenter #获得QWidget窗口的大小 size = Self.geometry ( www.caibaoyule.cn ) #将窗口移动到屏幕中间 Self.move ((Screen.width ()-size.width ())/2, (Screen.height ()-size.height ())/2) def onbuttonclick (self): # sender is the sender of the signaling object sender = Self.sender () print (Sender.text (www.jyz521.com/) +" was pressed ') Qapp = Qapplication.instance () qapp.quit (www.365soke.cn) if __name__ = = ' __main__ ': app = qapplication (sys.argv) Form = WinForm () form.show () Sys.exit (App.exec_ () )
PyQt5 Learning-Basic window control--qmainwindow