├── README ├── btn.py ├── label.py ├── dlg.py ├── dbus_c.py ├── dbus_s.py └── app.py /README: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /btn.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from PySide.QtCore import * 3 | from PySide.QtGui import * 4 | 5 | def sayHello(): 6 | print "Hello World!" 7 | 8 | # Create the Qt Application 9 | app = QApplication(sys.argv) 10 | # Create a button, connect it and show it 11 | button = QPushButton("Click me") 12 | button.clicked.connect(sayHello) 13 | button.show() 14 | # Run the main Qt loop 15 | app.exec_() 16 | -------------------------------------------------------------------------------- /label.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | # Import PySide classes 4 | import sys 5 | from PySide.QtCore import * 6 | from PySide.QtGui import * 7 | 8 | 9 | # Create a Qt application 10 | app = QApplication(sys.argv) 11 | # Create a Label and show it 12 | # label = QLabel("Hello World") 13 | label = QLabel("Hello World") 14 | label.show() 15 | 16 | # Enter Qt application main loop 17 | app.exec_() 18 | sys.exit() 19 | -------------------------------------------------------------------------------- /dlg.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import sys 5 | from PySide.QtCore import * 6 | from PySide.QtGui import * 7 | 8 | class Form(QDialog): 9 | 10 | def __init__(self, parent=None): 11 | super(Form, self).__init__(parent) 12 | # Create widgets 13 | self.edit = QLineEdit("Write my name here") 14 | self.button = QPushButton("Show Greetings") 15 | # Create layout and add widgets 16 | layout = QVBoxLayout() 17 | layout.addWidget(self.edit) 18 | layout.addWidget(self.button) 19 | # Set dialog layout 20 | self.setLayout(layout) 21 | # Add button signal to greetings slot 22 | self.button.clicked.connect(self.greetings) 23 | 24 | # Greets the user 25 | def greetings(self): 26 | print ("Hello %s" % self.edit.text()) 27 | 28 | 29 | if __name__ == '__main__': 30 | # Create the Qt Application 31 | app = QApplication(sys.argv) 32 | # Create and show the form 33 | form = Form() 34 | form.show() 35 | # Run the main Qt loop 36 | sys.exit(app.exec_()) 37 | -------------------------------------------------------------------------------- /dbus_c.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | # DBUS Client using PySide integration 5 | 6 | import sys 7 | from traceback import print_exc 8 | 9 | # import python dbus module 10 | import dbus 11 | # import python dbus GLib mainloop support 12 | import dbus.mainloop.glib 13 | # import QtCore 14 | from PySide.QtCore import * 15 | 16 | # signal handler 17 | def button_clicked(): 18 | print "button clicked" 19 | 20 | # main function 21 | if __name__ == '__main__': 22 | 23 | # Enable glib main loop support 24 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 25 | # Get the session bus 26 | bus = dbus.SessionBus() 27 | 28 | try: 29 | # Get the remote object 30 | remote_object = bus.get_object("com.example.SampleService", 31 | "/DBusWidget") 32 | # Get the remote interface for the remote object 33 | iface = dbus.Interface(remote_object, "com.example.SampleWidget") 34 | except dbus.DBusException: 35 | print_exc() 36 | sys.exit(1) 37 | 38 | # Start the application 39 | app = QCoreApplication([]) 40 | 41 | # Call some methods of the remote interface 42 | iface.show() 43 | iface.setText("Emit signal") 44 | # connect the DBus signal clicked to the function button_clicked 45 | iface.connect_to_signal("clicked", button_clicked) 46 | iface.connect_to_signal("lastWindowClosed", app.quit) 47 | 48 | # enter in the main loop 49 | app.exec_() 50 | -------------------------------------------------------------------------------- /dbus_s.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | # DBUS Server Example of use PySide with PyDBus library 5 | 6 | import dbus 7 | import dbus.service 8 | import dbus.mainloop.glib 9 | import random 10 | 11 | from PySide.QtCore import * 12 | from PySide.QtGui import QPushButton, QApplication 13 | 14 | # The adaptor, MUST inherit dbus.service.Object 15 | class DBusWidget(dbus.service.Object): 16 | def __init__(self, name, session): 17 | # export this object to dbus 18 | dbus.service.Object.__init__(self, name, session) 19 | 20 | # create a simple widget 21 | self.widget = QPushButton() 22 | self.widget.resize(200, 50) 23 | 24 | # To export a Qt signal as a DBus-signal, you need to connect it to a method in this class. 25 | # The method MUST have the signal annotation, so python-dbus will export it as a dbus-signal 26 | QObject.connect(self.widget, SIGNAL("clicked()"), self.clicked) 27 | QObject.connect(QApplication.instance(), SIGNAL("lastWindowClosed()"), self.lastWindowClosed) 28 | 29 | # You can export methods to dbus like you do in python-dbus. 30 | @dbus.service.method("com.example.SampleWidget", in_signature='', out_signature='') 31 | def show(self): 32 | self.widget.show() 33 | 34 | # Another method... now with a parameter 35 | @dbus.service.method("com.example.SampleWidget", in_signature='s', out_signature='') 36 | def setText(self, value): 37 | self.widget.setText(value) 38 | 39 | # Another one... 40 | @dbus.service.method("com.example.SampleWidget", in_signature='', out_signature='') 41 | def exit(self): 42 | qApp().quit() 43 | 44 | # A signal that will be exported to dbus 45 | @dbus.service.signal("com.example.SampleWidget", signature='') 46 | def clicked(self): 47 | pass 48 | 49 | # Another signal that will be exported to dbus 50 | @dbus.service.signal("com.example.SampleWidget", signature='') 51 | def lastWindowClosed(self): 52 | pass 53 | 54 | 55 | if __name__ == '__main__': 56 | app = QApplication([]) 57 | # Use qt/glib mainloop integration to get dbus mainloop working 58 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 59 | 60 | session_bus = dbus.SessionBus() 61 | # Export the service 62 | name = dbus.service.BusName("com.example.SampleService", session_bus) 63 | # Export the object 64 | widget = DBusWidget(session_bus, '/DBusWidget') 65 | 66 | print "Running example service." 67 | app.exec_() 68 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import platform 3 | 4 | import PySide 5 | 6 | from PySide.QtCore import QRect 7 | from PySide.QtGui import QApplication, QMainWindow, QTextEdit, QPushButton,\ 8 | QMessageBox, QIcon, QAction, QWidget, QGridLayout,\ 9 | QTextEdit, QMenuBar, QMenu, QStatusBar 10 | 11 | __version__ = '0.0.0' 12 | # import qrc_combine 13 | 14 | class MainWindow(QMainWindow): 15 | def __init__(self, parent=None): 16 | super(MainWindow, self).__init__(parent) 17 | # self.setObjectName("MainWindow") 18 | self.resize(731, 475) 19 | centralwidget = QWidget(self) 20 | # centralwidget.setObjectName("centralwidget") 21 | gridLayout = QGridLayout(centralwidget) 22 | # gridLayout.setObjectName("gridLayout") 23 | # textEdit needs to be a class variable. 24 | self.textEdit = QTextEdit(centralwidget) 25 | # self.textEdit.setObjectName("textEdit") 26 | gridLayout.addWidget(self.textEdit, 0, 0, 1, 1) 27 | self.setCentralWidget(centralwidget) 28 | menubar = QMenuBar(self) 29 | menubar.setGeometry(QRect(0, 0, 731, 29)) 30 | # menubar.setObjectName("menubar") 31 | menu_File = QMenu(menubar) 32 | # menu_File.setObjectName("menu_File") 33 | self.setMenuBar(menubar) 34 | statusbar = QStatusBar(self) 35 | # statusbar.setObjectName("statusbar") 36 | self.setStatusBar(statusbar) 37 | actionShow_GPL = QAction(self) 38 | # actionShow_GPL.setObjectName("actionShow_GPL") 39 | actionShow_GPL.triggered.connect(self.showGPL) 40 | action_About = QAction(self) 41 | # action_About.setObjectName("action_About") 42 | action_About.triggered.connect(self.about) 43 | iconToolBar = self.addToolBar("iconBar.png") 44 | #------------------------------------------------------ 45 | # Add icons to appear in tool bar - step 1 46 | actionShow_GPL.setIcon(QIcon(":/showgpl.png")) 47 | action_About.setIcon(QIcon(":/about.png")) 48 | action_Close = QAction(self) 49 | action_Close.setCheckable(False) 50 | action_Close.setObjectName("action_Close") 51 | action_Close.setIcon(QIcon(":/quit.png")) 52 | #------------------------------------------------------ 53 | # Show a tip on the Status Bar - step 2 54 | actionShow_GPL.setStatusTip("Show GPL Licence") 55 | action_About.setStatusTip("Pop up the About dialog.") 56 | action_Close.setStatusTip("Close the program.") 57 | #------------------------------------------------------ 58 | menu_File.addAction(actionShow_GPL) 59 | menu_File.addAction(action_About) 60 | menu_File.addAction(action_Close) 61 | menubar.addAction(menu_File.menuAction()) 62 | 63 | iconToolBar.addAction(actionShow_GPL) 64 | iconToolBar.addAction(action_About) 65 | iconToolBar.addAction(action_Close) 66 | action_Close.triggered.connect(self.close) 67 | 68 | def showGPL(self): 69 | '''Read and display GPL licence.''' 70 | self.textEdit.setText(open('COPYING.txt').read()) 71 | 72 | def about(self): 73 | '''Popup a box with about message.''' 74 | QMessageBox.about(self, "About PyQt, Platform and the like", 75 | """ About this program v %s 76 |
Copyright 2011 Your Name. 77 | All rights reserved in accordance with 78 | GPL v2 or later - NO WARRANTIES! 79 |
This application can be used for 80 | displaying OS and platform details. 81 |
Python %s - PySide version %s - Qt version %s on %s""" % \ 82 | (__version__, platform.python_version(), PySide.__version__,\ 83 | PySide.QtCore.__version__, platform.system())) 84 | 85 | if __name__ == '__main__': 86 | app = QApplication(sys.argv) 87 | frame = MainWindow() 88 | frame.show() 89 | sys.exit(app.exec_()) 90 | --------------------------------------------------------------------------------