Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'explicitly': 0.05; '-*-': 0.07; 'column': 0.07; 'sys': 0.07; 'utf-8': 0.07; '128': 0.09; 'coding:': 0.09; 'exit': 0.09; 'prevents': 0.09; 'try:': 0.09; 'width': 0.09; 'window.': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'gui': 0.12; 'changes': 0.15; 'windows': 0.15; '**kwargs)': 0.16; '**kwargs):': 0.16; 'buttons,': 0.16; 'dict': 0.16; 'm.show()': 0.16; 'skip:q 30': 0.16; 'spacing': 0.16; 'specifying': 0.16; 'widgets.': 0.16; 'size,': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'skip:p 40': 0.19; 'import': 0.22; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; 'adds': 0.24; "aren't": 0.24; 'form:': 0.24; 'space.': 0.24; 'text,': 0.24; 'header': 0.24; 'looks': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; 'player': 0.26; 'right.': 0.26; 'read,': 0.26; 'skip:_ 20': 0.27; 'header:In- Reply-To:1': 0.27; 'appear': 0.29; 'michael': 0.29; 'skip:p 30': 0.29; 'scale': 0.29; "doesn't": 0.30; "i'm": 0.30; 'work.': 0.31; 'asked': 0.31; 'code': 0.31; 'everywhere': 0.31; 'file:': 0.31; 'horizontal': 0.31; 'received:207.126': 0.31; 'skip:q 20': 0.31; 'skip:s 70': 0.31; 'file': 0.32; 'class': 0.32; 'this.': 0.32; 'running': 0.33; 'table': 0.34; 'skip:_ 10': 0.34; 'subject:with': 0.35; 'basic': 0.35; 'created': 0.35; 'except': 0.35; 'problem.': 0.35; 'skip:s 30': 0.35; 'skip:u 20': 0.35; 'add': 0.35; 'there': 0.35; 'height': 0.36; 'skip:s 60': 0.36; 'doing': 0.36; 'shows': 0.36; "i'll": 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'arrange': 0.38; 'skip:m 40': 0.38; 'window': 0.38; 'bad': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'according': 0.40; 'skip:u 10': 0.60; 'tell': 0.60; 'helps': 0.61; 'making': 0.63; 'show': 0.63; 'by:': 0.65; 'to:addr:gmail.com': 0.65; 'media': 0.66; 'here': 0.66; 'side': 0.67; 'bottom': 0.67; 'url:png': 0.68; '20,': 0.68; 'album': 0.68; 'covers': 0.68; 'skip:m 50': 0.68; 'default': 0.69; 'skip:r 30': 0.69; 'websites': 0.72; 'url:i': 0.72; 'hoping': 0.75; 'click': 0.77; 'beside': 0.84; 'phil': 0.84; 'skip:p 80': 0.84; 'song': 0.84; 'subject:Using': 0.84; 'url:imgur': 0.84; 'songs': 0.91; 'children.': 0.93; 'picture': 0.97; '2013': 0.98 X-Virus-Scanned: Debian amavisd-new at rvrbank1.user.openhosting.com MIME-Version: 1.0 Date: Thu, 22 Aug 2013 09:26:17 +0100 From: Phil Thompson To: Michael Staggs Subject: Re: Using PyQT with QT Designer Organization: Riverbank Computing Limited In-Reply-To: <1900623.ob0ITRdHWt@tannhaus-pc> References: <1900623.ob0ITRdHWt@tannhaus-pc> X-Sender: phil@riverbankcomputing.com User-Agent: RoundCube Webmail/0.3.1 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 185 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1377160367 news.xs4all.nl 15891 [2001:888:2000:d::a6]:59762 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52819 On Wed, 21 Aug 2013 21:04:47 -0500, Michael Staggs wrote: > I'm learning Python and I have a problem. I've asked the question > everywhere > and no one helps me, so I'm hoping someone here will. I am making a > program > that shows album covers and you click on the album cover in the top > window. In > the bottom window, the list of songs appear and you can click the > individual > song to play it. It's going to be a media player for children. I'm > thinking > I'll be able to use a dict and have the album as the key and the list of > songs > as the value to accomplish this. > > Right now, I'm just using my picture directory to try and get the basic > layout > right. I designed a form in QT Designer: http://i.imgur.com/Wrp1zHW.png > > Here is my gui file I got from running pyuic4 on the ui file: > > > # -*- coding: utf-8 -*- > > # Form implementation generated from reading ui file 'window.ui' > # > # Created by: PyQt4 UI code generator 4.9.6 > # > # WARNING! All changes made in this file will be lost! > > from PyQt4 import QtCore, QtGui > > try: > _fromUtf8 = QtCore.QString.fromUtf8 > except AttributeError: > def _fromUtf8(s): > return s > > try: > _encoding = QtGui.QApplication.UnicodeUTF8 > def _translate(context, text, disambig): > return QtGui.QApplication.translate(context, text, disambig, > _encoding) > except AttributeError: > def _translate(context, text, disambig): > return QtGui.QApplication.translate(context, text, disambig) > > class Ui_MainWindow(object): > def setupUi(self, MainWindow): > MainWindow.setObjectName(_fromUtf8("MainWindow")) > MainWindow.resize(800, 600) > self.centralwidget = QtGui.QWidget(MainWindow) > self.centralwidget.setObjectName(_fromUtf8("centralwidget")) > self.tableWidget = QtGui.QTableWidget(self.centralwidget) > self.tableWidget.setGeometry(QtCore.QRect(70, 20, 661, 381)) > self.tableWidget.setObjectName(_fromUtf8("tableWidget")) > self.tableWidget.setColumnCount(0) > self.tableWidget.setRowCount(0) > self.listWidget = QtGui.QListWidget(self.centralwidget) > self.listWidget.setGeometry(QtCore.QRect(70, 400, 661, 181)) > self.listWidget.setObjectName(_fromUtf8("listWidget")) > MainWindow.setCentralWidget(self.centralwidget) > > self.retranslateUi(MainWindow) > QtCore.QMetaObject.connectSlotsByName(MainWindow) > > def retranslateUi(self, MainWindow): > MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", > None)) > > Now, according to websites I read, I should just have to add the following > to > my program to get it to use the form: > > from window import Ui_MainWindow > class MainWindow(QMainWindow, Ui_MainWindow): > def __init__(self, parent=None, **kwargs): > super(MainWindow, self).__init__(parent) > self.setupUi(self) > > and here is my program: > > from PyQt4.QtCore import * > from PyQt4.QtGui import * > from window import Ui_MainWindow > > THUMBNAIL_SIZE = 128 > SPACING = 10 > IMAGES_PER_ROW = 5 > > class TableWidget(QTableWidget): > def __init__(self, parent=None, **kwargs): > QTableWidget.__init__(self, parent, **kwargs) > > self.setIconSize(QSize(128,128)) > self.setColumnCount(IMAGES_PER_ROW) > self.setGridStyle(Qt.NoPen) > > # Set the default column width and hide the header > self.verticalHeader().setDefaultSectionSize(THUMBNAIL_SIZE+SPACING) > self.verticalHeader().hide() > > # Set the default row height and hide the header > self.horizontalHeader().setDefaultSectionSize(THUMBNAIL_SIZE+SPACING) > self.horizontalHeader().hide() > > # Set the table width to show all images without horizontal > scrolling > self.setMinimumWidth((THUMBNAIL_SIZE+SPACING)*IMAGES_PER_ROW+(SPACING*2)) > > def addPicture(self, row, col, picturePath): > item=QTableWidgetItem() > > # Scale the image by either height or width and then 'crop' it to > the > # desired size, this prevents distortion of the image. > p=QPixmap(picturePath) > if p.height()>p.width(): p=p.scaledToWidth(THUMBNAIL_SIZE) > else: p=p.scaledToHeight(THUMBNAIL_SIZE) > p=p.copy(0,0,THUMBNAIL_SIZE,THUMBNAIL_SIZE) > item.setIcon(QIcon(p)) > > self.setItem(row,col,item) > > class MainWindow(QMainWindow, Ui_MainWindow): > def __init__(self, parent=None, **kwargs): > super(MainWindow, self).__init__(parent) > self.setupUi(self) > > centralWidget=QWidget(self) > l=QVBoxLayout(centralWidget) > > self.tableWidget=TableWidget(self) > l.addWidget(self.tableWidget) > > self.setCentralWidget(centralWidget) > > picturesPath=QDesktopServices.storageLocation(QDesktopServices.PicturesLocation) > pictureDir=QDir(picturesPath) > pictures=pictureDir.entryList(['*.jpg','*.png','*.gif']) > > rowCount=len(pictures)//IMAGES_PER_ROW > if len(pictures)%IMAGES_PER_ROW: rowCount+=1 > self.tableWidget.setRowCount(rowCount) > > row=-1 > for i,picture in enumerate(pictures): > col=i%IMAGES_PER_ROW > if not col: row+=1 > self.tableWidget.addPicture(row, col, > pictureDir.absoluteFilePath(picture)) > > if __name__=="__main__": > from sys import argv, exit > > a=QApplication(argv) > m=MainWindow() > m.show() > m.raise_() > exit(a.exec_()) > > But, it doesn't work. It ignores the form and the two windows go from side > to > side in the frame. When I add buttons, it adds them on TOP of those two > windows instead of beside them in the empty space....because there is no > empty > space. > > http://i.imgur.com/ZQfsMDa.png > > Please tell me what I'm doing wrong. It looks like you aren't using a layout to arrange your widgets. Explicitly specifying geometries is a bad idea. Phil