sebcbien 0 Newbie Poster

Hello

I'm currently working on a program which read 16bit(565) BMP files.

I've tried hard to read them with pyQt and show them directly but it never worked properly, the image were not read properly even with the good format as argument.

So I've decided to call a wx App to show them but the problem I have now occurs when I launch a new wx App, the Qt window get stuck and the lineEdit are not updated as long as I don't press a button into the Qt window.

I'd like to find a way to update the lineEdit field even when a wx App pop out.

An other problem occurs when I close the Qt window before the wx window, python is still running after closing both window in this order... Don't know why...

For the reading of the files, I've used a part of the code from Scru which help me to quickly write this little program. Thank you Scru.

There is three file in my program:


BMP_WAV.py

import ui_BMP_WAV
import ui_Extension
import sys


def doBMP_WAV():
    ui = ui_Extension.Ui_Extension()
    sys.exit(ui.app.exec_())

if __name__ == '__main__':
    doBMP_WAV()

ui_BMP_WAV.py (the Qt window created with Qt designer)

import os
from PyQt4 import QtCore, QtGui

class Ui_BMP_WAV_Window(QtGui.QMainWindow):
    def setupUi(self, BMP_WAV_Window):
        BMP_WAV_Window.setObjectName("BMP_WAV_Window")
        BMP_WAV_Window.resize(684, 710)
        self.centralwidget = QtGui.QWidget(BMP_WAV_Window)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout_4 = QtGui.QHBoxLayout(self.centralwidget)
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.TabWidget = QtGui.QTabWidget(self.centralwidget)
        self.TabWidget.setMovable(False)
        self.TabWidget.setObjectName("TabWidget")
        self.bmpTab = QtGui.QWidget()
        self.bmpTab.setObjectName("bmpTab")
        self.verticalLayout_7 = QtGui.QVBoxLayout(self.bmpTab)
        self.verticalLayout_7.setObjectName("verticalLayout_7")
        self.verticalLayout_6 = QtGui.QVBoxLayout()
        self.verticalLayout_6.setObjectName("verticalLayout_6")
        self.label = QtGui.QLabel(self.bmpTab)
        self.label.setPixmap(QtGui.QPixmap("Mire.bmp"))# doesn't work with 16bits BMP 565 files even whith the use of Image class, firstly design to show the open file...
        self.label.setObjectName("label")
        self.verticalLayout_6.addWidget(self.label)
        self.verticalLayout_5 = QtGui.QVBoxLayout()
        self.verticalLayout_5.setObjectName("verticalLayout_5")
        self.horizontalLayout_5 = QtGui.QHBoxLayout()
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.pixelWidthLabel = QtGui.QLabel(self.bmpTab)
        self.pixelWidthLabel.setMaximumSize(QtCore.QSize(60, 16777215))
        self.pixelWidthLabel.setObjectName("pixelWidthLabel")
        self.horizontalLayout.addWidget(self.pixelWidthLabel)
        self.xSizeLineEdit = QtGui.QLineEdit(self.bmpTab)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.xSizeLineEdit.sizePolicy().hasHeightForWidth())
        self.xSizeLineEdit.setSizePolicy(sizePolicy)
        self.xSizeLineEdit.setMaximumSize(QtCore.QSize(40, 16777215))
        self.xSizeLineEdit.setObjectName("xSizeLineEdit")
        self.horizontalLayout.addWidget(self.xSizeLineEdit)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtGui.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.pixelHighLabel = QtGui.QLabel(self.bmpTab)
        self.pixelHighLabel.setMaximumSize(QtCore.QSize(60, 16777215))
        self.pixelHighLabel.setObjectName("pixelHighLabel")
        self.horizontalLayout_2.addWidget(self.pixelHighLabel)
        self.ySizeLineEdit = QtGui.QLineEdit(self.bmpTab)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.ySizeLineEdit.sizePolicy().hasHeightForWidth())
        self.ySizeLineEdit.setSizePolicy(sizePolicy)
        self.ySizeLineEdit.setMaximumSize(QtCore.QSize(40, 16777215))
        self.ySizeLineEdit.setObjectName("ySizeLineEdit")
        self.horizontalLayout_2.addWidget(self.ySizeLineEdit)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_5.addLayout(self.verticalLayout)
        spacerItem = QtGui.QSpacerItem(500, 20, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_5.addItem(spacerItem)
        self.verticalLayout_5.addLayout(self.horizontalLayout_5)
        self.verticalLayout_4 = QtGui.QVBoxLayout()
        self.verticalLayout_4.setObjectName("verticalLayout_4")
        self.horizontalLayout_3 = QtGui.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.verticalLayout_3 = QtGui.QVBoxLayout()
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        spacerItem1 = QtGui.QSpacerItem(20, 17, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.verticalLayout_3.addItem(spacerItem1)
        self.loadBmpButton = QtGui.QPushButton(self.bmpTab)
        self.loadBmpButton.setObjectName("loadBmpButton")
        self.verticalLayout_3.addWidget(self.loadBmpButton)
        self.horizontalLayout_3.addLayout(self.verticalLayout_3)
        self.verticalLayout_2 = QtGui.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.bmpNameLabel = QtGui.QLabel(self.bmpTab)
        self.bmpNameLabel.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.bmpNameLabel.setObjectName("bmpNameLabel")
        self.verticalLayout_2.addWidget(self.bmpNameLabel)
        self.bmpFileNameLineEdit = QtGui.QLineEdit(self.bmpTab)
        self.bmpFileNameLineEdit.setObjectName("bmpFileNameLineEdit")
        self.verticalLayout_2.addWidget(self.bmpFileNameLineEdit)
        self.horizontalLayout_3.addLayout(self.verticalLayout_2)
        self.verticalLayout_4.addLayout(self.horizontalLayout_3)
        self.procBmpButton = QtGui.QPushButton(self.bmpTab)
        self.procBmpButton.setObjectName("procBmpButton")
        self.verticalLayout_4.addWidget(self.procBmpButton)
        self.verticalLayout_5.addLayout(self.verticalLayout_4)
        self.verticalLayout_6.addLayout(self.verticalLayout_5)
        self.verticalLayout_7.addLayout(self.verticalLayout_6)
        self.TabWidget.addTab(self.bmpTab, "")
        self.wavTab = QtGui.QWidget()
        self.wavTab.setObjectName("wavTab")
        self.TabWidget.addTab(self.wavTab, "")
        self.horizontalLayout_4.addWidget(self.TabWidget)
        BMP_WAV_Window.setCentralWidget(self.centralwidget)
        self.statusbar = QtGui.QStatusBar(BMP_WAV_Window)
        self.statusbar.setObjectName("statusbar")
        BMP_WAV_Window.setStatusBar(self.statusbar)

        self.retranslateUi(BMP_WAV_Window)
        self.TabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(BMP_WAV_Window)

    def retranslateUi(self, BMP_WAV_Window):
        BMP_WAV_Window.setWindowTitle(QtGui.QApplication.translate("BMP_WAV_Window", "BMP WAV to C array application", None, QtGui.QApplication.UnicodeUTF8))
        self.pixelWidthLabel.setText(QtGui.QApplication.translate("BMP_WAV_Window", "Pixels width", None, QtGui.QApplication.UnicodeUTF8))
        self.xSizeLineEdit.setText(QtGui.QApplication.translate("BMP_WAV_Window", "123", None, QtGui.QApplication.UnicodeUTF8))
        self.pixelHighLabel.setText(QtGui.QApplication.translate("BMP_WAV_Window", "Pixels high", None, QtGui.QApplication.UnicodeUTF8))
        self.ySizeLineEdit.setText(QtGui.QApplication.translate("BMP_WAV_Window", "123", None, QtGui.QApplication.UnicodeUTF8))
        self.loadBmpButton.setText(QtGui.QApplication.translate("BMP_WAV_Window", "load BMP File", None, QtGui.QApplication.UnicodeUTF8))
        self.bmpNameLabel.setText(QtGui.QApplication.translate("BMP_WAV_Window", "BMP File Name", None, QtGui.QApplication.UnicodeUTF8))
        self.procBmpButton.setText(QtGui.QApplication.translate("BMP_WAV_Window", "Create .C File", None, QtGui.QApplication.UnicodeUTF8))
        self.TabWidget.setTabText(self.TabWidget.indexOf(self.bmpTab), QtGui.QApplication.translate("BMP_WAV_Window", "BMP", None, QtGui.QApplication.UnicodeUTF8))
        self.TabWidget.setTabText(self.TabWidget.indexOf(self.wavTab), QtGui.QApplication.translate("BMP_WAV_Window", "WAV", None, QtGui.QApplication.UnicodeUTF8))

ui_Extension.py which extend the Qt window and add function and is used to create wx App.

from ui_BMP_WAV import Ui_BMP_WAV_Window
from PyQt4 import QtGui, QtCore
import sys, os, wx
class Ui_Extension(Ui_BMP_WAV_Window):
    def __init__ (self):
        self.app = QtGui.QApplication(sys.argv)
        QtGui.QMainWindow.__init__(self)
        self.window = QtGui.QMainWindow()
        self.setupUi(self.window)
        self.createSignals()
        self.window.show()
        self.curdir = os.getcwd()
        (self.xsize,self.ysize) = ("0","0")
        
        self.imageapp = showBMP()
        self.imageapp.MainLoop()
        
    def createSignals(self):
        QtCore.QObject.connect(self.loadBmpButton, QtCore.SIGNAL("clicked()"), self.pushedLoadBmpButton)
        QtCore.QObject.connect(self.procBmpButton, QtCore.SIGNAL("clicked()"), self.pushedProcBmpButton)
    def pushedLoadBmpButton(self):
        self.imagepath = QtGui.QFileDialog.getOpenFileName(self,"Select BMP", self.curdir, ("BMP (*.bmp)"))
        self.bmpFileNameLineEdit.setText(self.imagepath)
        (self.xsize,self.ysize) = self.getSize(self.imagepath)
        self.xSizeLineEdit.setText(self.xsize)
        self.ySizeLineEdit.setText(self.ysize)
        self.horizontalLayout_2.update()
        self.imageapp.show(str(self.imagepath))
        self.imageapp.updateImage()
        
        
    def pushedProcBmpButton(self):
        procOk = self.procBMP(self.imagepath)
        if procOk == 0:
            QtGui.QMessageBox.information(self,'Information','C File has been successfully created')
        
    def getSize(self,filepath): # get the header and return the size of the BMP -- should be rewrite somedays
        file = open(filepath,"rb")
        fileread = file.read()
        file.close()
        self.header = fileread[:71]
        xpixels = self.header[21:17:-1]
        xlist = []
        for c in xpixels:
            data = str(hex(ord(c)))
            if len(data) == 3:
                data = 'x0'.join(data.split('x'))
            xlist.append(data)
        xsize = int('0x'+''.join(''.join(xlist).split('0x')),base = 16)

        ypixels = self.header[25:21:-1]
        ylist = []
        for c in ypixels:
            data = str(hex(ord(c)))
            if len(data) == 3:
                data = 'x0'.join(data.split('x'))
            ylist.append(data)
        ysize = int('0x'+''.join(''.join(ylist).split('0x')),base = 16)
        return (str(xsize),str(ysize))
        
    def procBMP(self, filepath):
        return 0
        
class showBMP(wx.App):
    def __init__(self):
        wx.App.__init__(self)
        self.frame = wx.Frame(None, wx.ID_ANY, "Show an image file")
        self.panel = wx.Panel(self.frame)
    def show(self, image_file = ''):
            self.image_file = image_file
            self.image = wx.Bitmap(self.image_file)
            image_width = self.image.GetWidth()
            image_height = self.image.GetHeight()
            # set frame size to image size
            self.frame.SetClientSize((image_width, image_height))
            # show the image as static bitmap
            #~ self.bitmap = wx.StaticBitmap(self.frame, wx.ID_ANY, image)
            self.bitmap = wx.StaticBitmap(self.panel, wx.ID_ANY, self.image)
            # show the image name in the title
            self.frame.SetTitle(self.image_file)
            self.frame.Show()
    def updateImage(self):
        self.bitmap.SetBitmap(self.image)
        self.panel.Refresh()

Thank you for your help !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.