GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Showing posts with label PyQt. Show all posts
Showing posts with label PyQt. Show all posts

Friday, April 1, 2011

PyQt - 2D Graphics

import sys
from PyQt4 import QtGui, QtCore

class Editor(QtGui.QWidget):

    def __init__(self):
        super(Editor, self).__init__()
        self.initUI()

    def initUI(self):

        self.setGeometry(200, 100, 400, 300)
        self.setWindowTitle('Editor App')
        self.setWindowIcon(QtGui.QIcon('e.png'))

    def paintEvent(self, e):
        qp = QtGui.QPainter()
        qp.begin(self)
        qp.drawRect(10,10,100,100)
        qp.end()



app = QtGui.QApplication(sys.argv)
file = Editor()
file.show()
sys.exit(app.exec_())
Share/Bookmark

PyQt - Editor

import sys
from PyQt4 import QtGui, QtCore

class Editor(QtGui.QWidget):

    def __init__(self):
        super(Editor, self).__init__()
        self.initUI()

    def initUI(self):

        self.setGeometry(200, 100, 400, 300)
        self.setWindowTitle('Editor App')
        self.setWindowIcon(QtGui.QIcon('e.png'))

        self.editor = QtGui.QTextEdit(self)
        self.editor.setGeometry(10, 10, 380, 250)

        self.openFile = QtGui.QPushButton('Open', self)
        self.saveFile = QtGui.QPushButton('Save As Plain Text', self)
        self.saveHTML = QtGui.QPushButton('Save As HTML', self)
        self.pbExit = QtGui.QPushButton('Exit', self)

        self.openFile.setGeometry(10, 270, 70, 20)
        self.saveFile.setGeometry(85, 270, 120, 20)
        self.saveHTML.setGeometry(210, 270, 100, 20)
        self.pbExit.setGeometry(315, 270, 70, 20)

        self.connect( self.openFile, QtCore.SIGNAL('clicked()'), self.openPlease )
        self.connect( self.saveFile, QtCore.SIGNAL('clicked()'), self.savePlease )
        self.connect( self.saveHTML, QtCore.SIGNAL('clicked()'), self.saveHtmlPlease )
        self.connect( self.pbExit, QtCore.SIGNAL('clicked()'), QtGui.qApp, QtCore.SLOT('quit()'))

    def openPlease(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
        fname = open(filename)
        data = fname.read()
        fname.close()
        self.editor.setText(data)

    def savePlease(self):
        filename = QtGui.QFileDialog.getSaveFileName(self, 'Save File', '.')
        fname = open(filename, 'w')
        fname.write(self.editor.toPlainText())
        fname.close()

    def saveHtmlPlease(self):
        filename = QtGui.QFileDialog.getSaveFileName(self, 'Save File', '.')
        fname = open(filename, 'w')
        fname.write(self.editor.toHtml())
        fname.close()


app = QtGui.QApplication(sys.argv)
file = Editor()
file.show()
sys.exit(app.exec_())

Share/Bookmark

PyQt - File Dialog

To get a filename using open file dialog, use:
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
fname = open(filename)
data = fname.read()
self.textEdit.setText(data)
fname.close()

While to save data to file, from example from textEdit:
filename = QtGui.QFileDialog.getSaveFileName(self, 'Save File', '.')
fname = open(filename, 'w')
fname.write(textEdit.toPlainText())
fname.close()
Share/Bookmark

PyQt - Message Box

QtGui.QMessageBox.information(self, 'Message Title', 'The Bosy Text', QtGui.MessageBox.No | QtGui.MessageBox.Yes || QtGui.MessageBox.Cancel)

The type of message:
1. information
2. question
3. warning
4. critical

The type of the button:
1. Yes
2. No
3. Save
4. Cancel
5. Discard
6. Ok
7. Open
Share/Bookmark

Thursday, March 31, 2011

PyQt - QComboBox

import sys
from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()

widget.setGeometry(200, 100, 400, 300)
widget.setWindowTitle('PyQt Application')

cb = QtGui.QComboBox(widget)
label = QtGui.QLabel(widget)

lang = ['Python','Java','Javascript','PHP','C','C++','Ruby']
for i in lang:
    cb.addItem(i)

cb.setGeometry(10, 10, 150, 20)
label.setGeometry(170, 10, 100, 20)

def hello(text):
    label.setText(text)

widget.connect(cb, QtCore.SIGNAL('activated(QString)'), hello)

widget.show()
sys.exit(app.exec_())

Share/Bookmark

Wednesday, March 30, 2011

PyQt - QLineEdit

import sys
from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()

widget.setGeometry(200, 100, 400, 300)
widget.setWindowTitle('PyQt Application')

save = QtGui.QPushButton('Save', widget)
name = QtGui.QLineEdit(widget)
show = QtGui.QLabel(widget)

name.setGeometry(10, 10, 200, 20)
save.setGeometry(10, 40, 80, 20)
show.setGeometry(100, 100, 200, 20)

def showText():
    show.setText(name.text())
widget.connect(save, QtCore.SIGNAL('clicked()'), showText)

widget.show()
sys.exit(app.exec_())

Share/Bookmark

pyQt - QProgressBar

import sys
from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()

widget.setGeometry(200, 100, 400, 300)
widget.setWindowTitle('PyQt Application')

pb = QtGui.QProgressBar(widget)
pb.setGeometry(10, 10, 300, 20)
pb.setValue(50)

widget.show()
sys.exit(app.exec_())

Share/Bookmark

PyQt - QSlider

import sys
from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()

widget.setGeometry(200, 100, 400, 300)
widget.setWindowTitle('PyQt Application')

slider = QtGui.QSlider(QtCore.Qt.Horizontal, widget)
slider.setGeometry(10, 10, 200, 30)
slider.setFocusPolicy(QtCore.Qt.NoFocus)

def getValue(value):
    print value
widget.connect(slider, QtCore.SIGNAL('valueChanged(int)'), getValue)

widget.show()
sys.exit(app.exec_())

Share/Bookmark

PyQt - QCheckBox

import sys
from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()

widget.setGeometry(200, 100, 400, 300)
widget.setWindowTitle('PyQt Application')


show = QtGui.QCheckBox('Ayam Bakar',widget)
show.move(10, 10)

def hello():
    if show.isChecked():
        widget.setWindowTitle('Ayam Bakar')
    else:
        widget.setWindowTitle('')
       
widget.connect(show, QtCore.SIGNAL('stateChanged(int)'), hello)

widget.show()
sys.exit(app.exec_())

Share/Bookmark

PyQt - Multiple CheckBox

import sys
from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()

widget.setGeometry(200, 100, 400, 300)
widget.setWindowTitle('PyQt Application')

foods = ['Ayam Bakar','Lalapan','Soto','Sate'];
len = len(foods)
cb = []
a = 0

for i in foods:
    cb.append(QtGui.QCheckBox(i, widget))
    a = a + 1;
   
a = 0
for i in foods:
    cb[a].move(10, 20*a)
    a = a + 1

def hello():
    for i in range(0,len):
        if cb[i].isChecked():
            widget.setWindowTitle(foods[i])
            break;
        else:
            widget.setWindowTitle('None')
           
for i in range(0,len):
    widget.connect(cb[i], QtCore.SIGNAL('stateChanged(int)'), hello)

widget.show()
sys.exit(app.exec_())

Share/Bookmark

PyQt - QPushButton

import sys
from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()

widget.setGeometry(200, 100, 400, 300)
widget.setWindowTitle('PyQt Application')

close = QtGui.QPushButton(QtGui.QIcon('lucia.png'), 'Close', widget)
close.setGeometry(10, 10, 100, 30)

def hello():
    print 'hello everybody'

widget.connect(close, QtCore.SIGNAL('clicked()'), hello)

widget.show()
sys.exit(app.exec_())



Share/Bookmark

PyQt - QWidget

import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()

#widget.resize(400, 300)
widget.setGeometry(200, 100, 400, 300)
widget.setWindowTitle('PyQt Application')
widget.setWindowIcon(QtGui.QIcon('lucia_2d.png'))
widget.setToolTip('Indonesia is a nice one')
widget.setStyleSheet('background: blue')
widget.setWindowStyle(0.5)

widget.show()
sys.exit(app.exec_())

Share/Bookmark

PyQt - Introduction

Below is the basic setting for displaying the app using PyQt:

import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()

widget.show()
sys.exit(app.exec_())
Share/Bookmark

PyQt - Reference

QtGui Reference:
1. Label
   label = QtGui.QLabel('Lucia Corp', widget)
   label.move(10, 10)
2. Button
   button = QtGui.QPushButton('Close', widget)
   button.setGeometry(10, 10, 80, 30)
3. Textarea
    textEdit = QtGui.QTextEdit()
    widget.setCentralWidget(textEdit)
4. Input Dialog
    text, ok = QtGui.QInputDialog.getText(widget, 'Login', 'Enter your name:')
5. Color Dialog
    color = QtGui.QColorDialog.getColor()
6. Font Dialog
    font, okey = QtGui.QFontDialog.getFont()
7. File Dialog
    filename = QtGui.QFileDialog.getOpenFileName(widget, 'Open File', 'E:\')
8. Calendar
    calendar = QtGui.QCalendarWidget(widget)
    print calendar.selectedDate()
9. Pixmap
    pixmap = QtGui.QPixmap('lucia.png')
    label.setPixmap(pixmap)
Share/Bookmark