Start New Discussion within our Software Development Community

i finally started having a look at gui yesterday, and tried to do some things with PySide, however i have the following problem;

Whats wrong with this code?

!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PySide.QtCore import *
from PySide.QtGui import *

class Form(QDialog):

def __init__(self, parent=None):
super(Form, self).__init__(parent)
# Create widgets
self.edit = QLineEdit("Write my name here")
self.button = QPushButton("Show Greetings") 
# Create layout and add widgets
layout = QVBoxLayout()
layout.addWidget(self.edit)
layout.addWidget(self.button)
# Set dialog layout
self.setLayout(layout)
# Add button signal to greetings slot
self.button.clicked.connect(self.greetin…
# Add button signal to exit slot
exit_button = QPushButton("Exit")
exit_button.clicked.connect(sys.exit)
# Greets the user
def greetings(self):
print ("Hello %s" % self.edit.text()) 


if __name__ == '__main__':
# Create the Qt Application
app = QApplication(sys.argv)
# Create and show the form
form = Form()
form.show()
# Run the main Qt loop
sys.exit(app.exec_())

p.s im only just started learning pyside, i dont know how to make a button to exit.

p.p.s i have been playing with python for a while now, and still dont think i know much at all, am i doing okay so far or am i really bad like i feel?!
also, i dont know if im going about this the best way, can someone give me some key progress goals to go by or something, so that i dont end up missing things or getting too far ahead too soon, for my own good.

You have to make sure your code blocks are indented properly.

Here is an example showing the proper indentations:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# PySide is the official LGPL-licensed version of PyQT
# I downloaded and used the Windows self-extracting installer
# PySide-1.0.0qt472.win32-py2.7.exe
# from: http://developer.qt.nokia.com/wiki/PySide_Binaries_Windows
# tested with pyside472 and Python27
 
from PySide.QtCore import *
from PySide.QtGui import *
 
class MyFrame(QWidget):
    def __init__(self, parent=None):
        # create the frame/window (this will be instance self)
        QWidget.__init__(self, parent)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(100, 150, 300, 50)
        self.setWindowTitle('button connected to a label')

        # create a button
        self.button = QPushButton("Click me")
        # when clicked connect button to method action()
        self.button.clicked.connect(self.action)

        # create 2 Qt labels
        self.label1 = QLabel()
        self.label2 = QLabel()

        # use grid layout to position the 3 widgets
        grid = QGridLayout()
        # addWidget(QWidget, row, column, rowSpan=1, columnSpan=1)
        grid.addWidget(self.button, 0, 0)
        grid.addWidget(self.label1, 1, 0)
        grid.addWidget(self.label2, 2, 0)

        self.setLayout(grid)
        
    def action(self):
        s = "You clicked the button!"
        self.label1.setText(s)
        # for optional color use HTML code
        html = "<font color=red>You clicked the button!</font>"
        self.label2.setText(html)
        # optional wave sound
        # sound file (.wav files only) should be in working folder
        # or give full file path 
        QSound.play("boing.wav")
 
# create the Qt Application
app = QApplication([])

frame = MyFrame()
frame.show()

# run the main Qt event loop
app.exec_()

You have to make sure your code blocks are indented properly.

Here is an example showing the proper indentations:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# PySide is the official LGPL-licensed version of PyQT
# I downloaded and used the Windows self-extracting installer
# PySide-1.0.0qt472.win32-py2.7.exe
# from: http://developer.qt.nokia.com/wiki/PySide_Binaries_Windows
# tested with pyside472 and Python27
 
from PySide.QtCore import *
from PySide.QtGui import *
 
class MyFrame(QWidget):
    def __init__(self, parent=None):
        # create the frame/window (this will be instance self)
        QWidget.__init__(self, parent)
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(100, 150, 300, 50)
        self.setWindowTitle('button connected to a label')

        # create a button
        self.button = QPushButton("Click me")
        # when clicked connect button to method action()
        self.button.clicked.connect(self.action)

        # create 2 Qt labels
        self.label1 = QLabel()
        self.label2 = QLabel()

        # use grid layout to position the 3 widgets
        grid = QGridLayout()
        # addWidget(QWidget, row, column, rowSpan=1, columnSpan=1)
        grid.addWidget(self.button, 0, 0)
        grid.addWidget(self.label1, 1, 0)
        grid.addWidget(self.label2, 2, 0)

        self.setLayout(grid)
        
    def action(self):
        s = "You clicked the button!"
        self.label1.setText(s)
        # for optional color use HTML code
        html = "<font color=red>You clicked the button!</font>"
        self.label2.setText(html)
        # optional wave sound
        # sound file (.wav files only) should be in working folder
        # or give full file path 
        QSound.play("boing.wav")
 
# create the Qt Application
app = QApplication([])

frame = MyFrame()
frame.show()

# run the main Qt event loop
app.exec_()

sorry, i previously posted it on another forum but when i copied it, it lost the indentation. I double checked my indentation and this still doesn't work for me, thank you for your time though.

Like this with correct indentations.

I double checked my indentation and this still doesn't work for me,

Do not post that code dont work,python always give you Traceback.
Next time post code and Traceback,then is`t much eaiser to help.
Understanding Traceback is very important part of programming in python.
1 line you get syntax error because of missing #

Also code from HiHe works,so now you have a couple working example with pyside.

#!/usr/bin/python 
# -*- coding: utf-8 -*-

import sys
from PySide.QtCore import *
from PySide.QtGui import *

class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        # Create widgets
        self.edit = QLineEdit("Write my name here")
        self.button = QPushButton("Show Greetings")
        # Create layout and add widgets
        layout = QVBoxLayout()
        layout.addWidget(self.edit)
        layout.addWidget(self.button)
        # Set dialog layout
        self.setLayout(layout)
        # Add button signal to greetings slot
        self.button.clicked.connect(self.greetings)
        # Add button signal to exit slot
        exit_button = QPushButton("Exit")
        exit_button.clicked.connect(sys.exit)

    # Greets the user
    def greetings(self):
        print ("Hello %s" % self.edit.text())

if __name__ == '__main__':
    # Create the Qt Application
    app = QApplication([])
    # Create and show the form
    form = Form()
    form.show()
    # Run the main Qt loop
    sys.exit(app.exec_())
Comments
good point
This article has been dead for over six months. Start a new discussion instead.