PyQt基本操作

twaby47sq 9年前

来自: http://www.cnblogs.com/rollenholt/archive/2011/11/16/2251904.html


PyQt的简介和基本安装方法读者可以自行google解决。先声明,本文章教基础,参考《征服Python》相关章节。不过不得不说,pyQt的教程真的好少,╮(╯▽╰)╭,悲催,大家有什么好的资料推荐一下,谢谢了。

先建立一个基本的界面看看:

1
2
3
4
5
6
7
8
9
10
11
12
import sys
from PyQt4 import  QtCore, QtGui
class MyWindow( QtGui.QMainWindow ):
     def __init__( self ):
         QtGui.QMainWindow.__init__( self )
         self .setWindowTitle( "PyQt" )
         self .resize( 300 , 200 )
app = QtGui.QApplication( sys.argv )
mywindow = MyWindow()
mywindow.show()
app.exec_()
    

 运行结果:

然后我们添加一个标签:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#coding=utf-8
#标签的使用
import sys
from PyQt4 import QtCore, QtGui
class Window( QtGui.QMainWindow ):
     def __init__( self ):
         super ( Window, self ).__init__()
         self .setWindowTitle( "hello" )
         self .resize( 200 , 300 )
         #添加标签
         label = QtGui.QLabel( "label" )
         label.setAlignment( QtCore.Qt.AlignCenter )
         self .setCentralWidget( label )
app = QtGui.QApplication( sys.argv )
demo = Window()
demo.show()
app.exec_()

 效果如下:

基本布局:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#coding=gbk
import sys
from PyQt4 import QtCore, QtGui
class Window( QtGui.QWidget ):
     def __init__( self ):
         super ( Window, self ).__init__()
         self .setWindowTitle( "hello" )
         self .resize( 500 , 500 )
         
         hBoxLayout1 = QtGui.QHBoxLayout()
         
         label1 = QtGui.QLabel( "Label1" )
         label2 = QtGui.QLabel( "Label2" )
         label3 = QtGui.QLabel( "Label3" )
         label4 = QtGui.QLabel( "Label4" )
         label5 = QtGui.QLabel( "Label5" )
         #---------添加表格布局
         gridLayout = QtGui.QGridLayout()
          
         gridLayout.addWidget( label1 , 0 , 0 )
         gridLayout.addWidget( label2 , 0 , 1 )
         gridLayout.addWidget( label3 , 0 , 2 )
         gridLayout.addWidget( label4 , 1 , 0 )
         gridLayout.addWidget( label5 , 1 , 1 )
          
         self .setLayout( gridLayout )       
#-------添加水平布局
#        hBoxLayout1.addWidget( label1 )
#        hBoxLayout1.addWidget( label2 )
#        hBoxLayout1.addWidget( label3 )
#        hBoxLayout1.addWidget( label4 )
#        hBoxLayout1.addWidget( label5 )
#       
#        self.setLayout( hBoxLayout1 )
 
#---------添加垂直布局
#        vBoxLayout = QtGui.QVBoxLayout()
#        vBoxLayout.addWidget( label1 )
#        vBoxLayout.addWidget( label2 )
#        vBoxLayout.addWidget( label3 )
#        vBoxLayout.addWidget( label4 )
#        vBoxLayout.addWidget( label5 )
#       
#        self.setLayout( vBoxLayout )
#       
 
 
app = QtGui.QApplication( sys.argv )
demo = Window()
demo.show()
app.exec_()

 效果:

按钮基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#coding=utf-8
#按钮操作
 
import sys
from PyQt4 import QtGui, QtCore
class Window( QtGui.QWidget ):
     def __init__( self ):
         super ( Window, self ).__init__()
         self .setWindowTitle( "hello" )
         self .resize( 500 , 500 )
         gridlayout = QtGui.QGridLayout()
         
         button1 = QtGui.QPushButton( "button1" )
         gridlayout.addWidget( button1, 0 , 0 , 1 , 3 )
         
         button2 = QtGui.QPushButton( "button2" )
         button2.setFlat( True )
         gridlayout.addWidget( button2, 1 , 1 , 1 , 3 )
         self .setLayout( gridlayout )
app = QtGui.QApplication( sys.argv )
demo = Window()
demo.show()
app.exec_()

 

不过按钮2当你点击的时候才显示出来哦

现在看看单行文本和多行文本

#coding=utf-8
import sys
from PyQt4 import QtGui, QtCore
class Window( QtGui.QWidget ):
     def __init__( self ):
         super ( Window, self ).__init__()
         self .setWindowTitle( "hello" )
         self .resize( 500 , 500 )
         
         gridlayout = QtGui.QGridLayout()
         
         str = "hello"  #这里中文乱码,纠结
         label = QtGui.QLabel( str )
         label.setAlignment( QtCore.Qt.AlignCenter )
         
         textFile = QtGui.QLineEdit()
         gridlayout.addWidget( label, 0 , 0 )
         gridlayout.addWidget( textFile )
         
         passwordFile = QtGui.QLineEdit()
         passwordFile.setEchoMode( QtGui.QLineEdit.Password )
         gridlayout.addWidget( passwordFile )
         
         textArea = QtGui.QTextEdit()
         textArea.setText( "asdasda" )
         gridlayout.addWidget( textArea )
         
         
         self .setLayout( gridlayout )
app = QtGui.QApplication( sys.argv )
window = Window()
window.show()
app.exec_()

 

单选和复选框

import sys
from PyQt4 import QtGui, QtCore
class Window( QtGui.QWidget ):
     def __init__( self ):
         super ( Window, self ).__init__()
         self .setWindowTitle( "hello" )
         self .resize( 500 , 500 )
         hboxlayout = QtGui.QHBoxLayout()
         
         self .radio1 = QtGui.QRadioButton( "radio1" )
         self .radio2 = QtGui.QRadioButton( "radio2" )
         self .radio3 = QtGui.QRadioButton( "radio3" )
         self .radio1.setChecked( True )
         
         hboxlayout.addWidget( self .radio1 )
         hboxlayout.addWidget( self .radio2 )
         hboxlayout.addWidget( self .radio3 )
         
         checkbox1 = QtGui.QCheckBox( "checkbox1" )
         checkbox2 = QtGui.QCheckBox( "checkbox2" )
         checkbox3 = QtGui.QCheckBox( "checkbox3" )
         checkbox1.setChecked( True )
         
         hboxlayout.addWidget( checkbox1 )
         hboxlayout.addWidget( checkbox2 )
         hboxlayout.addWidget( checkbox3 )
 
         self .button = QtGui.QPushButton( "Ok" )
         hboxlayout.addWidget( self .button )
         
         self .connect( self .button, QtCore.SIGNAL( 'clicked()' ), self .OnButton )
         
         self .setLayout( hboxlayout )
     def OnButton( self ):
         if self .radio2.isChecked():
             self .radio2.setText( "haha" )
         
app = QtGui.QApplication( sys.argv )
win = Window()
win.show()
app.exec_()

 

现在看看菜单:

#coding=utf-8
#菜单事件
import sys
from PyQt4 import QtGui, QtCore
class Window( QtGui.QMainWindow ):
     def __init__( self ):
         super ( Window, self ).__init__()
         self .setWindowTitle( "hello" )
         self .resize( 500 , 500 )
         
         
         
         menubar = self .menuBar()
         self . file = menubar.addMenu( '&File' )
         open = self . file .addAction( 'Open' )
         self .connect( open , QtCore.SIGNAL( 'triggered()' ), self .OnOpen )
         
         save = self . file .addAction( 'Save' )
         self .connect( save, QtCore.SIGNAL( 'triggered()' ), self .OnSave )
         self . file .addSeparator()
         close = self . file .addAction( "Close" )
         self .connect( close, QtCore.SIGNAL( 'triggered()' ), self .OnClose )
         
         self .label = QtGui.QLabel( "this is a  google test" )
         self .label.setAlignment( QtCore.Qt.AlignCenter )
         self .setCentralWidget( self .label )
         
     def OnOpen( self ):
         self .label.setText( "open" )
     def OnSave( self ):
         self .label.setText( "save" )
     def OnClose( self ):
         self .close()
     def contextMenuEvent( self , event ):
        self . file .exec_( event.globalPos() )
app = QtGui.QApplication( sys.argv )
win = Window()
win.show()
app.exec_()

 

点击右键也可以看看哦

现在是对话框的代码:

#coding=utf-8
#对话框
import sys
from PyQt4 import QtGui, QtCore
class Window( QtGui.QWidget ):
     def __init__( self ):
         super ( Window, self ).__init__()
         self .setWindowTitle( "hello" )
         self .resize( 500 , 500 )
         
         gridlayout = QtGui.QGridLayout()
         
         self .AboutButton = QtGui.QPushButton( "About" )
         gridlayout.addWidget( self .AboutButton, 0 , 0 )
         self .AboutQtButton = QtGui.QPushButton( "AboutQt" )
         gridlayout.addWidget( self .AboutQtButton, 0 , 1 )
         self .CriticalButton = QtGui.QPushButton( "CriticalButton" )
         gridlayout.addWidget( self .CriticalButton, 1 , 0 )
         self .InfoButton = QtGui.QPushButton( "Info" )
         gridlayout.addWidget( self .InfoButton, 1 , 1 )
         self .QuestionButton = QtGui.QPushButton( "Question" )
         gridlayout.addWidget( self .QuestionButton, 2 , 0 )
         self .WarningButton = QtGui.QPushButton( "Warning" )
         gridlayout.addWidget( self .WarningButton, 2 , 1 )
         
         spacer = QtGui.QSpacerItem( 200 , 80 )
         gridlayout.addItem( spacer, 3 , 1 , 1 , 5 )
         self .setLayout( gridlayout )
         
         self .connect( self .AboutButton, QtCore.SIGNAL( 'clicked()' ), self .OnAboutButton )
         self .connect( self .AboutQtButton, QtCore.SIGNAL( 'clicked()' ), self .OnAboutQtButton )
         self .connect( self .CriticalButton, QtCore.SIGNAL( 'clicked()' ), self .OnCriticalButton )
         self .connect( self .InfoButton, QtCore.SIGNAL( 'clicked()' ), self .OnInfoButton )
         self .connect( self .QuestionButton, QtCore.SIGNAL( 'clicked()' ), self .OnQuestionButton )
         self .connect( self .WarningButton, QtCore.SIGNAL( 'clicked()' ), self .OnWarningButton )
         
     def OnAboutButton( self ):
         QtGui.QMessageBox.about( self , 'PyQt' , "About" )
         
     def OnAboutQtButton( self ):
         QtGui.QMessageBox.aboutQt( self , "PyQt" )
         
     def OnCriticalButton( self ):
         r = QtGui.QMessageBox.critical( self , "PyQT" , "CriticalButton" , QtGui.QMessageBox.Abort,
                                    QtGui.QMessageBox.Retry, QtGui.QMessageBox.Ignore )
         if r = = QtGui.QMessageBox.Abort:
             self .setWindowTitle( "Abort" )
         elif r = = QtGui.QMessageBox.Retry:
             self .setWindowTitle( "Retry" )
         elif r = = QtGui.QMessageBox.Ignore:
             self .setWindowTitle( "Ignore" )
         else :
             pass
         
     def OnInfoButton( self ):
         QtGui.QMessageBox.information( self , "Pyqt" , "information" )
         
     def OnQuestionButton( self ):
         r = QtGui.QMessageBox.question( self , "PyQt" , "Question" , QtGui.QMessageBox.Yes, QtGui.QMessageBox.No, QtGui.QMessageBox.Cancel )
         
     def OnWarningButton( self ):
         r = QtGui.QMessageBox.warning( self , "PyQT" , "warning" , QtGui.QMessageBox.Yes, QtGui.QMessageBox.No )
     
         
app = QtGui.QApplication( sys.argv )
win = Window()
win.show()
app.exec_()

 

现在是文件选择,字体选择,颜色选择框:

#coding=utf-8
import sys
from PyQt4 import QtGui, QtCore
class Window( QtGui.QWidget ):
     def __init__( self ):
         super ( Window, self ).__init__()
         self .setWindowTitle( "hello" )
         self .resize( 500 , 500 )
         
         gridlayout = QtGui.QGridLayout()
         
         self .button1 = QtGui.QPushButton( "File" )
         self .button2 = QtGui.QPushButton( "Font" )
         self .button3 = QtGui.QPushButton( "Color" )
         gridlayout.addWidget( self .button1 )
         gridlayout.addWidget( self .button2 )
         gridlayout.addWidget( self .button3 )
         spacer = QtGui.QSpacerItem( 200 , 80 )
         gridlayout.addItem( spacer, 3 , 1 , 1 , 3 )
         self .setLayout( gridlayout )
         
         self .connect( self .button1, QtCore.SIGNAL( 'clicked()' ), self .OnButton1 )
         self .connect( self .button2, QtCore.SIGNAL( 'clicked()' ), self .OnButton2 )
         self .connect( self .button3, QtCore.SIGNAL( 'clicked()' ), self .OnButton3 )
         
         
     def OnButton1( self ):
         fileName = QtGui.QFileDialog.getOpenFileName( self , 'Open' )
         if not fileName.isEmpty():
             self .setWindowTitle( fileName )   
         
     def OnButton2( self ):
         font, ok = QtGui.QFontDialog.getFont()
         if ok:
             self .setWindowTitle( font.key() )
     
     def OnButton3( self ):
         color = QtGui.QColorDialog.getColor()
         if color.isValid():
             self .setWindowTitle( color.name() )
         
app = QtGui.QApplication( sys.argv )
win = Window()
win.show()
app.exec_()

 

现在还是对话框:

#coding=utf-8
import sys
from PyQt4 import QtGui, QtCore
 
class MyDialog( QtGui.QDialog ):
     def __init__( self ):
         super ( MyDialog, self ).__init__()
         
         self .setWindowTitle( "Dialog" )
         self .gridlayout = QtGui.QGridLayout()
         
         self .label = QtGui.QLabel( "Please Input:" )
         self .textField = QtGui.QLineEdit()
         self .okButton = QtGui.QPushButton( "OK" )
         self .cancalButton = QtGui.QPushButton( "Cancel" )
         
         self .gridlayout.addWidget( self .label , 0 , 0 )
         self .gridlayout.addWidget( self .textField , 0 , 1 )
         self .gridlayout.addWidget( self .cancalButton , 0 , 2 )
         self .gridlayout.addWidget( self .okButton , 0 , 3 )
         
         self .connect( self .okButton, QtCore.SIGNAL( 'clicked()' ), self .OnOk )
         self .connect( self .cancalButton, QtCore.SIGNAL( 'clicked()' ), self .OnCancel )
         
         self .setLayout( self .gridlayout )
         
     def OnOk( self ):
         self .text = self .textField.text()
         self .done( 1 )
     def OnCancel( self ):
         self .done( 0 )
         
 
class Window( QtGui.QWidget ):
     def __init__( self ):
         super ( Window, self ).__init__()
         self .setWindowTitle( "hello" )
         self .resize( 400 , 300 )
         
         hboxlayout = QtGui.QGridLayout()
         self .creatDialogButton = QtGui.QPushButton( "Create a new Dialog" )
         hboxlayout.addWidget( self .creatDialogButton )
         self .setLayout( hboxlayout )
         
         self .connect( self .creatDialogButton, QtCore.SIGNAL( 'clicked()' ), self .OnButton )
         
     def OnButton( self ):
         dialog = MyDialog()
         r = dialog.exec_()
         if r:
             self .creatDialogButton.setText( dialog.text )
         
         
app = QtGui.QApplication( sys.argv )
win = Window()
win.show()
app.exec_()

 

最后一个是利用Designer写的

利用他设计一个简单的界面,只有一个标签和一个文本框

代码如下:

#coding=utf-8
import sys
from PyQt4 import QtGui, QtCore, uic
 
class MyDialog( QtGui.QDialog ):
     def __init__( self ):
         super ( MyDialog, self ).__init__()
         uic.loadUi( "res.ui" , self )
 
class MyWindow( QtGui.QWidget ):
     def __init__( self ):
         super ( MyWindow, self ).__init__()
         self .setWindowTitle( "hello" )
         self .resize( 300 , 200 )
         
         gridlayout = QtGui.QGridLayout()
         self .button = QtGui.QPushButton( "CreateDialog" )
         gridlayout.addWidget( self .button )
         self .setLayout( gridlayout )
         
         self .connect( self .button, QtCore.SIGNAL( 'clicked()' ), self .OnButtoN )
         
     def OnButtoN( self ):
         dialog = MyDialog()
         r = dialog.exec_();
         if r:
             self .button.setText( dialog.textField.text() )
         
app = QtGui.QApplication( sys.argv )
demo = MyWindow()
demo.show()
app.exec_()