import qt
#! / usr / bin / python import sys from qt import * # create an application and pass arguments a = QApplication (sys.argv) # create widget # The first argument is the text we want to see. The second argument is the parent widget, # because Hello - the only widget, then he has no parent hello = QLabel ("Hello world!", None) # make the widget mainly a.setMainWidget (hello) # show widget hello.show () #launch the application a.exec_loop ()
#! / usr / bin / python # coding = utf-8 import sys from qt import * a = QApplication (sys.argv) # Our function to be called when a button is pressed def sayHello (): print "Hello, World!" # create button hellobutton = QPushButton ("Say 'Hello world!'", None) # assign a click handler to the button a.connect (hellobutton, SIGNAL ("clicked ()"), sayHello) # assign a button to the main widget a.setMainWidget (hellobutton) # show shows hellobutton.show () #launch the application a.exec_loop ()
#! / usr / bin / python # coding = UTF-8 import sys from qt import * # class inherits from QApplication class HelloApplication (QApplication): def __init __ (self, args): "" " In the constructor, we do everything we need to run our application, which creates a QApplication in the __init__ method, then adds our widgets, and finally runs exec_loop "" " QApplication .__ init __ (self, args) self.addWidgets () self.exec_loop () def addWidgets (self): "" "In this method, we add widgets and attach signal handlers. The signal handler for the widget is also called a "slot" "" " self.hellobutton = QPushButton ("Say 'Hello, World!'", None) self.connect (self.hellobutton, SIGNAL ("clicked ()"), self.slotSayHello) self.setMainWidget (self.hellobutton) self.hellobutton.show () def slotSayHello (self): "" " This is an example of a slot - a method that is called when a signal comes. "" " print "Hello, World!" # This script should only be executed separately, so we have to check it, # but we should also be able to connect this program without running any code if __name__ == "__main__": app = HelloApplication (sys.argv)
pyuic testapp_ui.ui -o testapp_ui.py
#! / usr / bin / python # coding = utf-8 from testapp_ui import TestAppUI from qt import * import sys class HelloApplication (QApplication): def __init __ (self, args): "" " In the constructor, we do everything that is necessary to run our application, which creates a QApplication in the __init__ method, then adds our widgets, and finally runs exec_loop "" " QApplication .__ init __ (self, args) # We pass None because this top level widget self.maindialog = TestApp (None) self.setMainWidget (self.maindialog) self.maindialog.show () self.exec_loop () class TestApp (TestAppUI): def __init __ (self, parent): # Run the parent constructor and attach the slots to the methods TestAppUI .__ init __ (self, parent) self._connectSlots () # Initially, the list is empty, so the delete button should not work # Make it inactive self.deletebutton.setEnabled (False) def _connectSlots (self): # Install handlers on the buttons self.connect (self.addbutton, SIGNAL ("clicked ()"), self._slotAddClicked) self.connect (self.lineedit, SIGNAL ("returnPressed ()"), self._slotAddClicked) self.connect (self.deletebutton, SIGNAL ("clicked ()"), self._slotDeleteClicked) def _slotAddClicked (self): # Read teskt from lineedit, text = self.lineedit.text () # if lineedit is not empty, if len (text): # insert a new item in the list ... lvi = QListViewItem (self.listview) # with text from lineedit ... lvi.setText (0, text) # and clear the lineedit. self.lineedit.clear () # Delete button m. off, so turn it on. self.deletebutton.setEnabled (True) def _slotDeleteClicked (self): # Remove the selected item from the list self.listview.takeItem (self.listview.currentItem ()) # Check if the list is empty - if yes, disable the deletebutton. # If the list after this was empty, then we will make the delete button inactive if self.listview.childCount () == 0: self.deletebutton.setEnabled (False) if __name__ == "__main__": app = HelloApplication (sys.argv)
Source: https://habr.com/ru/post/31426/
All Articles