6 Replies - 9922 Views - Last Post: 07 September 2011 - 12:19 PM Rate Topic: -----

#1 poker158149  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 70
  • Joined: 29-June 11

GUI Programming for Python 3

Posted 03 September 2011 - 03:23 PM

Hi everyone. I've been looking into programming GUI's for a few programs I have, but I'm not sure where to start.

First off, which GUI framework for Python 3 should I use?

I've looked into Tkinter, but it's just not working for me.

I was interested in wxPython, but they don't have a version for Python 3.

I was also told about PyQt, which I'm currently looking into; but I can't find any tutorials or any help with it, even just a simple "Hello, World" program.

I was just wondering, which GUI framework do you guys use and/or suggest? Which are best for beginners?

Is This A Good Question/Topic? 0
  • +

Replies To: GUI Programming for Python 3

#2 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: GUI Programming for Python 3

Posted 03 September 2011 - 05:35 PM

I use PyQt for Python 3 work. You'll find that the framework is still the same as it was in Python 2, so you can still use all the version 2 tutorials, as long you're aware of the differences between 2 and 3.
Was This Post Helpful? 0
  • +
  • -

#3 poker158149  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 70
  • Joined: 29-June 11

Re: GUI Programming for Python 3

Posted 03 September 2011 - 05:47 PM

I was pulling towards using PyQt.

I found a couple of sample scripts and some of them used PyQt 4.5, where I have PyQt 4.6 installed, and it said something about not being able to use "QVariant()." How would you go about replacing that?
Was This Post Helpful? 0
  • +
  • -

#4 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: GUI Programming for Python 3

Posted 03 September 2011 - 06:14 PM

Hmm well I'm using PyQt 4.8, and the QVariant class is still there. Could you post what the messages are?
Was This Post Helpful? 0
  • +
  • -

#5 poker158149  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 70
  • Joined: 29-June 11

Re: GUI Programming for Python 3

Posted 03 September 2011 - 06:40 PM

This is the sample code for a simple table:

# use PyQT's QTableView and QAbstractTableModel
# to present tabular data (with column sort option)
# tested with Python 3.1 and PyQT 4.5

import operator
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MyWindow(QWidget):
    def __init__(self, element_list, header, *args):
        QWidget.__init__(self, *args)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(300, 200, 460, 300)
        self.setWindowTitle("Sorting PyQT's QTableView")

        self.header = header
        self.mydata = element_list
        # create table
        table = self.createTable()

        # use vbox layout
        layout = QVBoxLayout()
        layout.addWidget(table)
        self.setLayout(layout)

    def createTable(self):
        # create table view
        tview = QTableView()
        # set table model
        tmodel = MyTableModel(self, self.mydata, self.header)
        tview.setModel(tmodel)
        # set minimum size of table
        tview.setMinimumSize(450, 300)
        # hide grid
        tview.setShowGrid(False)
        # set font
        font = QFont("Courier New", 8)
        tview.setFont(font)
        # hide vertical header
        vh = tview.verticalHeader()
        vh.setVisible(False)
        # set horizontal header properties
        hh = tview.horizontalHeader()
        hh.setStretchLastSection(True)
        # set column width to fit contents
        tview.resizeColumnsToContents()
        # set all row heights
        nrows = len(self.mydata)
        for row in range(nrows):
            tview.setRowHeight(row, 18)
        # enable sorting
        tview.setSortingEnabled(True)
        return tview

class MyTableModel(QAbstractTableModel):
    def __init__(self, parent, mydata, header, *args):
        """
        mydata is list of tuples
        header is list of strings
        tuple length has to match header length
        """
        QAbstractTableModel.__init__(self, parent, *args)
        self.mydata = mydata
        self.header = header

    def rowCount(self, parent):
        return len(self.mydata)

    def columnCount(self, parent):
        return len(self.mydata[0])

    def data(self, index, role):
        if not index.isValid():
            return QVariant()
        elif role != Qt.DisplayRole:
            return QVariant()
        return QVariant(self.mydata[index.row()][index.column()])

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return QVariant(self.header[col])
        return QVariant()

    def sort(self, col, order):
        """sort table by given column number col"""
        self.emit(SIGNAL("layoutAboutToBeChanged()"))
        self.mydata = sorted(self.mydata,
            key=operator.itemgetter(col))
        if order == Qt.DescendingOrder:
            self.mydata.reverse()
        self.emit(SIGNAL("layoutChanged()"))


# the test data is from one of vegaseat's codes
# so if there is a mistake don't blame Henri ...
header = [' Symbol ', ' Name ', ' Atomic Weight ',
    ' Melt (K) ', ' Boil (K) ']
# use numbers for numeric data to sort properly
element_list = [
('H', 'Hydrogen', 1.00794, 13.81, 20.28),
('He', 'Helium', 4.0026, 0.95, 4.216),
('Li', 'Lithium', 6.941, 453.7, 1615),
('Be', 'Beryllium', 9.0122, 1560, 3243),
('B', 'Boron', 10.811, 2365, 4275),
('C', 'Carbon', 12.011, 3825, 5100),
('N', 'Nitrogen', 14.0067, 63.15, 77.344),
('O', 'Oxygen', 15.9994, 54.8, 90.188),
('F', 'Fluorine', 18.9984, 53.65, 85.0),
('Ne', 'Neon', 20.1797, 24.55, 27.1),
('Na', 'Sodium', 22.98977, 371.0, 1156),
('Mg', 'Magnesium', 24.305, 922, 1380),
('Al', 'Aluminum', 26.9815, 933.5, 2740),
('Si', 'Silicon', 28.0855, 1683, 2630),
('P', 'Phosphorus', 30.9737, 317.3, 553),
('S', 'Sulfur', 32.066, 392.2, 717.62),
('Cl', 'Chlorine', 35.4527, 172.17, 239.18),
('Ar', 'Argon', 39.948, 83.95, 87.45),
('K', 'Potassium', 39.0983, 336.8, 1033),
('Ca', 'Calcium', 40.078, 1112, 1757),
('Sc', 'Scandium', 44.9559, 1814, 3109),
('Ti', 'Titanium', 47.88, 1935, 3560),
('V', 'Vanadium', 50.9415, 2136, 3650),
('Cr', 'Chromium', 51.996, 2130, 2945),
('Mn', 'Manganese', 54.938, 1518, 2235),
('Fe', 'Iron', 55.847, 1808, 3023),
('Co', 'Cobalt', 58.9332, 1768, 3143),
('Ni', 'Nickel', 58.6934, 1726, 3005),
('Cu', 'Copper', 63.546, 1356.6, 2840),
('Zn', 'Zinc', 65.39, 682.73, 1180),
('Ga', 'Gallium', 69.723, 302.92, 2478),
('Ge', 'Germanium', 72.61, 1211.5, 3107),
('As', 'Arsenic', 74.9216, 876.4, 876.3),
('Se', 'Selenium', 78.96, 494, 958),
('Br', 'Bromine', 79.904, 265.95, 331.85),
('Kr', 'Krypton', 83.8, 116, 120.85),
('Rb', 'Rubidium', 85.4678, 312.63, 961),
('Sr', 'Strontium', 87.62, 1042, 1655),
('Y', 'Yttrium', 88.9059, 1795, 3611),
('Zr', 'Zirconium', 91.224, 2128, 4683),
('Nb', 'Niobium', 92.9064, 2743, 5015),
('Mo', 'Molybdenum', 95.94, 2896, 4912),
('Tc', 'Technetium', 98, 2477, 4538),
('Ru', 'Ruthenium', 101.07, 2610, 4425),
('Rh', 'Rhodium', 102.9055, 2236, 3970),
('Pd', 'Palladium', 106.42, 1825, 3240),
('Ag', 'Silver', 107.868, 1235.08, 2436),
('Cd', 'Cadmium', 112.41, 594.26, 1040),
('In', 'Indium', 114.82, 429.78, 2350),
('Sn', 'Tin', 118.71, 505.12, 2876),
('Sb', 'Antimony', 121.757, 903.91, 1860),
('Te', 'Tellurium', 127.6, 722.72, 1261),
('I', 'Iodine', 126.9045, 386.7, 457.5),
('Xe', 'Xenon', 131.29, 161.39, 165.1),
('Cs', 'Cesium', 132.9054, 301.54, 944),
('Ba', 'Barium', 137.33, 1002, 2079),
('La', 'Lanthanum', 138.9055, 1191, 3737),
('Ce', 'Cerium', 140.12, 1071, 3715),
('Pr', 'Praseodymium', 140.9077, 1204, 3785),
('Nd', 'Neodymium', 144.24, 1294, 3347),
('Pm', 'Promethium', 145, 1315, 3273),
('Sm', 'Samarium', 150.36, 1347, 2067),
('Eu', 'Europium', 151.965, 1095, 1800),
('Gd', 'Gadolinium', 157.25, 1585, 3545),
('Tb', 'Terbium', 158.9253, 1629, 3500),
('Dy', 'Dysprosium', 162.5, 1685, 2840),
('Ho', 'Holmium', 164.9303, 1747, 2968),
('Er', 'Erbium', 167.26, 1802, 3140),
('Tm', 'Thulium', 168.9342, 1818, 2223),
('Yb', 'Ytterbium', 173.04, 1092, 1469),
('Lu', 'Lutetium', 174.967, 1936, 3668),
('Hf', 'Hafnium', 178.49, 2504, 4875),
('Ta', 'Tantalum', 180.9479, 3293, 5730),
('W', 'Tungsten', 183.85, 3695, 5825),
('Re', 'Rhenium', 186.207, 3455, 5870),
('Os', 'Osmium', 190.2, 3300, 5300),
('Ir', 'Iridium', 192.22, 2720, 4700),
('Pt', 'Platinum', 195.08, 2042.1, 4100),
('Au', 'Gold', 196.9665, 1337.58, 3130),
('Hg', 'Mercury', 200.59, 234.31, 629.88),
('Tl', 'Thallium', 204.383, 577, 1746),
('Pb', 'Lead', 207.2, 600.65, 2023),
('Bi', 'Bismuth', 208.9804, 544.59, 1837),
('Po', 'Polonium', 209, 527, 1235.15),
('At', 'Astatine', 210, 575, 610),
('Rn', 'Radon', 222, 202, 211.4),
('Fr', 'Francium', 223, 300, 950),
('Ra', 'Radium', 226.0254, 973, 1413),
('Ac', 'Actinium', 227, 1324, 3470),
('Th', 'Thorium', 232.0381, 2028, 5060),
('Pa', 'Proctactinium', 231.0359, 1845, 4300),
('U', 'Uranium', 238.029, 1408, 4407),
('Np', 'Neptunium', 237.0482, 912, 4175),
('Pu', 'Plutonium', 244, 913, 3505),
('Am', 'Americium', 243, 1449, 2880),
]

app = QApplication(sys.argv)
win = MyWindow(element_list, header)
win.show()
sys.exit(app.exec_())




The is what the poster posted with his code (it's not code, but I feel it'll be better organized in code tags):

Editor's Note:
With the advent of PyQT 4.6 the use of QVariant() should be avoided. So replace
return QVariant() with return None
return QVariant(self.header[col]) with return self.header[col]
and so on. 




And this is the error that get's spit out:

TypeError: PyQt4.QtCode.QVariant represents a mapped type and cannot be instantiated


Was This Post Helpful? 0
  • +
  • -

#6 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: GUI Programming for Python 3

Posted 04 September 2011 - 02:33 AM

Hmm, well, try not using the QVariant at all. I've never been big on it myself, it's more of a C++ construct, and isn't very useful for a dynamically typed language like Python.

But I'm not an authority on QVariant, and my use of Qt is typically pretty basic, so I don't particularly see a use for it myself.

Here's a huge list of Python Tutorial References which are particularly useful. In particular, I like Zetcode's tutorials.
Was This Post Helpful? 0
  • +
  • -

#7 poker158149  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 70
  • Joined: 29-June 11

Re: GUI Programming for Python 3

Posted 07 September 2011 - 12:19 PM

Ah, wow, thanks so much for the links. I'll definitely give those a look. Also, when I start GUI programming, I'll avoid the QVariant.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1