PyQt 界面控件常用积累

来源:互联网 发布:淘宝会员折扣没有了 编辑:程序博客网 时间:2024/06/05 08:16
代码

QtGui.QApplication(sys.argv) #QApplication 类初始化

sys.exit(app.exec_())           #进入消息循环,等待窗体退出


创建主界面的两种方法
1.通过继承QtGui.QMainWindow创建类
QtGui.QMainWindow.__init__(self)        # 调用父类初始化方法

2.通过继承QtGui.QWidget创建类
QtGui.QWidget.__init__(self)            # 调用父类初始化方法

QPushButton # 按钮
setFlat(True) #设置文件样式按钮

连接事件信号的两种方法
1.利用主界面self的connect方法
self.connect(self.button1,                     # Button1事件
    QtCore.SIGNAL('clicked()'),             # clicked()信号
    self.OnButton1)                            # 插槽函数
2.利用控件本身connect方法                        
button.clicked.connect(showdata)            



对话窗体基本用法                
class MyDialog(QtGui.QDialog):                # 继承QtGui.QDialog    
    ...
    self.done(1)                            # 结束对话框返回1
调用    
        dialog = MyDialog()                    # 创建对话框对象
        r = dialog.exec_()                    # 运行对话框
if r:
            self.button.setText(dialog.text)        
        
文本标签控件QLabel    
QtGui.QLabel('PyQt\nLabel')                    # 创建标签
label1.setAlignment(QtCore.Qt.AlignCenter)    # 居中对齐

单行文本框控件QLineEdit
edit1 = QtGui.QLineEdit()                    # 创建单行文本框
edit2.setEchoMode(QtGui.QLineEdit.Password)    # 将其设置为密码框

多行文本控件QTextEdit
edit = QtGui.QTextEdit()                    # 创建多行文本框
edit.setText('Python\nPyQt')                    # 设置文本框中的文字

表格式布局gridlayout
gridlayout.setRowMinimumHeight (1, 180)        # 设置第二行的最小高度为108



窗体菜单栏控件menuBar的基本用法
class MyWindow(QtGui.QMainWindow):            # 通过继承QtGui.QMainWindow创建类
    menubar = self.menuBar()                # 获得窗口的菜单条
    file = menubar.addMenu('&File')            # 添加File菜单
    file.addAction('Open')                    # 添加Open命令        
open = self.file.addAction('Open')            # 添加Open命令
self.connect(open, QtCore.SIGNAL('triggered()'), self.OnOpen)    # 菜单信号    

退出主窗体界面
self.close()

界面右键菜单用法
    def contextMenuEvent(self, event):            # 重载弹出式菜单事件
        self.file.exec_(event.globalPos())    
            
常用消息框用法            
QtGui.QMessageBox.about(self, 'PyQt', 'About')        # 创建About消息框
QtGui.QMessageBox.aboutQt(self, 'PyQt')                # 创建AboutQt消息框  

r = QtGui.QMessageBox.critical(self, 'PyQt',        # 创建Ctitical消息框
'Critical',
                QtGui.QMessageBox.Abort,
                QtGui.QMessageBox.Retry,
                QtGui.QMessageBox.Ignore)
返回状态判断                
            if r == QtGui.QMessageBox.Abort:
                    self.label.setText('Abort')
            elif r == QtGui.QMessageBox.Retry:
                   self.label.setText('Retry')
            else:
                    self.label.setText('Ignore')      
                    
QtGui.QMessageBox.information(self, 'PyQt', 'Information')    # 创建Information消息框

            r = QtGui.QMessageBox.question(self, 'PyQt',    # 创建Question消息框
'Question',
                QtGui.QMessageBox.Yes,
                QtGui.QMessageBox.No,
                QtGui.QMessageBox.Cancel)
                
               r = QtGui.QMessageBox.warning(self, 'PyQt',        # 创建Warning消息框
'Warning',
                QtGui.QMessageBox.Yes,
                QtGui.QMessageBox.No)

单选按钮复选按钮的用法                
        self.radio1 = QtGui.QRadioButton('Radio1')        # 创建单选框
        self.radio2 = QtGui.QRadioButton('Radio2')

        self.check = QtGui.QCheckBox('check')        # 创建复选框
        self.check.setChecked(True)                    # 将复选框选中
状态获取        
if self.radio1.isChecked():
if self.check.isChecked():

xml界面文件的用法
from PyQt4 import QtCore, QtGui, uic    
class MyDialog(QtGui.QDialog):                    # 继承QtGui.QDialog
def__init__(self):
        QtGui.QWidget.__init__(self)
            uic.loadUi("res.ui", self)    
            
    def OnButton(self):                            # 处理按钮事件        
        dialog = MyDialog()                        # 创建对话框对象
        r = dialog.exec_()                        # 运行对话框
if r:
            self.button.setText(dialog.lineEdit.text())  #获取对话框中控件元素的值

空白项控件QSpacerItem的用法            
spacer1 = QtGui.QSpacerItem(300,40)        # 创建空白项
gridlayout.addItem(spacer1, 0 ,0)        # 添加空白项

标准系统对话框用法
        filename = QtGui.QFileDialog.getOpenFileName(self, 'Open')    # 创建文件打开对话框
ifnot filename.isEmpty():
                    self.label.setText(filename)
                    
        font, ok = QtGui.QFontDialog.getFont()                # 创建字体选中对话框
if ok:
            self.label.setText(font.key())
            
        color = QtGui.QColorDialog.getColor()                # 创建颜色选择对话框
if color.isValid():
            self.label.setText(color.name())
0 0
原创粉丝点击