Python+PyQt,GUI操作

安装anaconda的朋友通过conda list 可以发现已经安装了PyQt.
下面开始gui
先建立一个基本的界面

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_()

添加一个label

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_()

添加多个label,排列方式可以自选,竖直或者水平或者按一定顺序

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_()

添加按钮:

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" )
#点击的时候显示按钮2
        button2.setFlat( True )

        gridlayout.addWidget( button2, 1, 1, 1, 3 )

        self.setLayout( gridlayout )

app = QtGui.QApplication( sys.argv )

demo = Window()

demo.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 )



        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_()

添加菜单:



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_() 



添加对话框:

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_()

给文字,字体,颜色添加连接

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_()

对话框,选项

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_()

运行代码即可看到效果图,试试?
所有的这些函数比如QtGui.QApplication,都可以通过help查询帮助文档。

感谢:(http://www.cnblogs.com/rollenholt/archive/2011/11/16/2251904.html)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z_shsf

来包瓜子嘛,谢谢客官~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值