pyqt4教程之实现windows窗口小示例分享

yipeiwu_com5年前Python基础

复制代码 代码如下:

import sys
from PyQt4 import QtGui, QtCore
class Window( QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle('hello')
        self.resize(800,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 text')
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.setCentralWidget(self.label)

    def OnOpen(self):
        self.label.setText('open')
    def OnClose(self):
        self.close()
    def OnSave( self):
        self.label.setText('save')
    def contextMenuEvent(self,event):
        self.file.exec_( event.globalPos())

app =QtGui.QApplication(sys.argv)
win = Window()
win.show()
app.exec_()

相关文章

Python使用functools模块中的partial函数生成偏函数

python 中提供一种用于对函数固定属性的函数(与数学上的偏函数不一样) # 通常会返回10进制 int('12345') # print 12345 # 使用参数 返回 8...

Python创建普通菜单示例【基于win32ui模块】

Python创建普通菜单示例【基于win32ui模块】

本文实例讲述了Python创建普通菜单的方法。分享给大家供大家参考,具体如下: 一、代码 # -*- coding:utf-8 -*- #! python3 import win32...

python批量修改文件夹及其子文件夹下的文件内容

python批量修改文件夹及其子文件夹下的文件内容

前言:前几天我看一位同学要修改很多文件中的数据,该文件数据很规律,一行只有三个数,需要将每行最后一个数字改为负数,但文件有上千个,分布在每个文件夹下面以及它的多级子文件夹下,看他用exc...

Python实现串口通信(pyserial)过程解析

pyserial模块封装了对串口的访问,兼容各种平台。 安装 pip insatll pyserial 初始化 简单初始化示例 import serial ser = se...

django项目登录中使用图片验证码的实现方法

django项目登录中使用图片验证码的实现方法

应用下创建untils文件夹放置封装图片验证码的函数 创建validCode.py文件定义验证码规则 import random def get_random_color():...