PYQT5 Tutorials--Components Ⅱ (eight)

Source: Internet
Author: User

This section of the tutorial will continue to introduce the components of PYQT5. The content of our tutorial will include a pixel chart (qpixmap), a single-line text box (Qlineedit), and a drop-down list box (Qcombobox)

Pixel Chart (QPIXMAP)

A pixel chart (QPIXMAP) is one of a variety of components for working with images. It is the best choice for displaying pictures on the screen. In our code example, we will use a window like Sotulai to display a picture.

#!/usr/bin/python3#-*-coding:utf-8-*-"" "Zetcode PyQt5 Tutorial In this example, we dispay an imageon the window. Author:jan Bodnarwebsite:zetcode.com last Edited:january "" "Import sysfrom pyqt5.qtwidgets import (Qwidget, Qhboxla Yout,     Qlabel, qapplication) from Pyqt5.qtgui import Qpixmapclass Example (qwidget):        def __init__ (self):        Super (). __init__ ()                Self.initui ()                    def initui (self):              Hbox = Qhboxlayout (self)        pixmap = Qpixmap (" Redrock.png ")        LBL = Qlabel (self)        lbl.setpixmap (pixmap)        hbox.addwidget (LBL)        self.setlayout (Hbox)                Self.move (self.setwindowtitle) (        ' Red Rock ')        self.show ()                        if __name__ = = ' __main__ ':        app = Qapplication (SYS.ARGV)    ex = Example ()    sys.exit (App.exec_ ())

To run this example, we will display a picture on the window.

Pixmap = Qpixmap ("Redrock.png")

Creates a Qpixmap object. The object construction method passes in the name of a file as a parameter.

LBL = Qlabel (self) lbl.setpixmap (pixmap)

We set the pixel graph object to the tag to display the pixel graph through the label.

Single-line text edit box (qlineedit)

The single-line text edit box component allows you to enter plain text data for a single line. This component supports Undo, redo, cut, paste, drag, and drag methods.

#!/usr/bin/python3#-*-coding:utf-8-*-"" "Zetcode PyQt5 Tutorial This example shows text which are entered in a Qlineedit In a Qlabel widget.      Author:jan Bodnarwebsite:zetcode.com last Edited:january "" "Import sysfrom pyqt5.qtwidgets import (Qwidget, Qlabel, Qlineedit, Qapplication) class Example (Qwidget): def __init__ (self): Super (). __init__ () s                Elf.initui () def initui (self): SELF.LBL = Qlabel (self) Qle = Qlineedit (self)                Qle.move (Self.lbl.move) qle.textchanged[str].connect (self.onchanged) Self.setgeometry (280) self.setwindowtitle (' Qlineedit ') self.show () def on  Changed (self, Text): Self.lbl.setText (text) self.lbl.adjustSize () if __name__ = = ' __main__ ': app = Qapplication (sys.argv) ex = Example () sys.exit (App.exec_ ())

This example shows a single-line Edit text box and a label. When we enter text in a single-line text edit box, the text is displayed synchronously in the label.

Qle = Qlineedit (self)

Creates a single-line text edit box (qlineedit) component.

Qle.textchanged[str].connect (self.onchanged)

Call the OnChanged () method if the text in the edit box is changed in a single line.

def onChanged (self, text):        self.lbl.setText (text)    

Above is the implementation of the OnChanged () method, we set the display text of the label. We call the Adjustsize () method to adjust the length of the label relative to the displayed text.

Figure:qlineedit

Split Box (Qsplitter)

The split box component lets us control the size of the subassembly by dragging and dropping the split line. In our example, we show the three qframe components constrained by the two split box components.

#!/usr/bin/python3#-*-coding:utf-8-*-"" "Zetcode PyQt5 Tutorial This example showshow to use Qsplitter widget. Author:jan Bodnarwebsite:zetcode.com last Edited:january "" "Import sysfrom pyqt5.qtwidgets import (Qwidget, Qhboxla Yout, Qframe, Qsplitter, Qstylefactory, qapplication) from Pyqt5.qtcore import Qtclass Example (qwidget): Def __i  Nit__ (self): Super (). __init__ () Self.initui () def initui (self): Hbox = Qhboxlayout (self) topleft = Qframe (self) topleft.setframeshape (qframe.styledpanel) topright = Qfr Ame (self) topright.setframeshape (qframe.styledpanel) bottom = Qframe (self) bottom.setframeshape (qfram E.styledpanel) Splitter1 = Qsplitter (qt.horizontal) splitter1.addwidget (topleft) Splitter1.addwidget ( TopRight) Splitter2 = Qsplitter (qt.vertical) splitter2.addwidget (splitter1) splitter2.addwidget (Botto m) Hbox.addwidget (SplitteR2) self.setlayout (Hbox) self.setgeometry (+, +) self.setwindowtitle (' Qsplitter ') ) self.show () def onChanged (self, Text): Self.lbl.setText (text) SELF.LBL.A Djustsize () if __name__ = = ' __main__ ': app = Qapplication (sys.argv) ex = Example () sys  . Exit (App.exec_ ())

In our example, we have three frame components and two split box components. Note under certain topics, the Split-box component may not be displayed.

TopLeft = Qframe (self) topleft.setframeshape (Qframe.styledpanel)

We used a style frame to make the split line between the frame components look obvious.

Splitter1 = Qsplitter (qt.horizontal) splitter1.addwidget (topleft) splitter1.addwidget (topright)

We created a split box component and added two frame components to the split box.

Splitter2 = Qsplitter (qt.vertical) splitter2.addwidget (splitter1)

We add the first split box to another splitter box component.

Figure:qsplitter Widget

Drop-down list box (Qcombobox)

The drop-down list box component allows the user to select a list item from the list.

#!/usr/bin/python3#-*-coding:utf-8-*-"" "Zetcode PyQt5 Tutorial This example shows what to use a Qcombobox widget.      Author:jan Bodnarwebsite:zetcode.com last Edited:january "" "Import sysfrom pyqt5.qtwidgets import (Qwidget, Qlabel, Qcombobox, Qapplication) class Example (Qwidget): def __init__ (self): Super (). __init__ () s Elf.initui () def initui (self): SELF.LBL = Qlabel ("Ubuntu", self) combo = Qcombobox ( Self) combo.additem ("Ubuntu") Combo.additem ("Mandriva") Combo.additem ("Fedora") Combo.additem ( "Arch") Combo.additem ("Gentoo") Combo.move (self.lbl.move) COMBO.ACTIVATED[STR] . Connect (self.onactivated) self.setgeometry (+) self.setwindowtitle (' Qcombo Box ') self.show () def onActivated (self, Text): Self.lbl.setText (text) SELF.L              Bl.adjustsize ()            if __name__ = = ' __main__ ': app = Qapplication (sys.argv) ex = Example () sys.exit (App.exec_ ()) 

A drop-down list box and a label are shown in the example. The drop-down list box has five list items. These five list items are the names of the Linux distributions. The label component displays the text of the list item that is selected in the drop-down list box.

Combo = Qcombobox (self) combo.additem ("Ubuntu") Combo.additem ("Mandriva") Combo.additem ("Fedora") Combo.additem (" Arch ") Combo.additem (" Gentoo ")

We created a drop-down list box and populated five list items.

Once the list item is selected, the Onactivated () method is called.

def onActivated (self, text):      self.lbl.setText (text)    

In the above method, we display the text of the list item selected in the drop-down list box on the label component. and the label size is adjusted according to the label text.

Figure:qcombobox

PYQT5 Tutorials--Components Ⅱ (eight)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.