Home arrow Python arrow PyQT: Input Widgets

PyQT: Input Widgets

You may have experienced some headaches in the past when creating GUIs to gather user input. Python makes this process easy with input widgets. Keep reading to learn more.

TABLE OF CONTENTS:
  1. PyQT: Input Widgets
  2. QCheckBox
  3. PyQT in the Real World
  4. The Connections
  5. The Main Part
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 4
November 20, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The edge that GUI has over CLI is the varied ways it offers to gather user input.  In a CLI-based application, there are at most two ways of taking the input – as strings of alphanumeric or as a single character or number. With a GUI-based application, the input can be gathered using various widgets such as textboxes, checkboxes, radio buttons etc.

In most GUI frameworks, input widgets are the most complex or difficult to create. But the simplicity of Python comes to the rescue with PyQT. There are many input widgets supplied with PyQT itself, and if the platform happens to be KDE, then widgets that are capable of handling multiple types of input strategies are available.

Starting from this part, I will be focusing on the different input widgets provided by PyQT. In this discussion I will be introducing the line input widget along with the checkbox widget. The first two sections will focus on the slots and signals of the widgets. The following sections will test the widgets by using them in a form that will later become part of a larger application. That sets the agenda for this discussion.

QLineEdit and QCheckbox: Understanding the Basic Widgets

Every GUI toolkit provides some widgets that provide basic services for gathering user inputs. These widgets don’t fit the category of heavy duty GUI widgets of the likes of a rich text editor, tree view et al. However they are quite indispensable when a pretty good user interface must be created. The single line textbox and checkbox are two of such basic yet indispensable widgets. In the world of PyQT they are known as QLineEdit and QCheckbox. 

The difference that these widgets have between their counterparts in other languages is the way events are handled. That’s right – signals and slots. So let's look at the most common signals and slots that are available for QLineEdit and QCheckbox.

QLineEdit

As the name suggests, QLineEdit is a one line text editor. In other words, it supports basic editing services but the text cannot span over multiple lines (read one line). In essence, it lets the user enter and edit a single line of plain text. The most common slots of this widget are:

  • setText()
  • selectAll()
  • setMaxLength()
  • insert()

and the signals are:

  • textChanged()
  • textEdited()

Let's look at the slots first.

The setText() slot is used to set a new text into the QLineEdit. When a new text is set, it removes the text that is already present. It takes one parameter – QString. When text is set using setText() method, the textChanged() signal is emitted.

The selectAll() slot is used to select all the entered text. That means this slot can be used to highlight the entered text. In some cases it becomes necessary to place default values for QLineEdit. But whenever the user tries to enter data, the default value has to be deleted. For this to take place, the default data present has to be completely highlighted so that it can be deleted when the user tries to enter data. Hence, to make the current data in the QLineEdit preselected, this slot can be called.

The setMaxLength() slot takes care of the requirement to set the maximum length of the text that can be entered. It takes an integer value as argument.

While the setText() slot replaces or appends the text (according to the logic used), there are times, when text needs to be inserted. The insert() slot comes handy in such situations. One thing to keep in mind is that while invoking this slot, if any  portion of the text is highlighted, it will be deleted and the value of the QString passed as argument will be pasted in place of that portion.

That’s about it for the theory of QLineEdit's service slots. Now let's look at how they can be used. The following block of code creates an object of QLineEdit, sets its length to 10, sets the text to “123- BackwardSt” and then selects it all:

Address = QLineEdit( )
Address.setMaxLength(10)
Address.setText(‘123- BackwardSt’)
Address.selectAll()

The next part of this puzzle is the signals emitted by QLineEdit.

Whenever the text is changed programmatically using the setText() slot, the textChanged() signal is emitted. This signal takes a QString argument which is the new text that has been set in QLineEdit. This is emitted when the text changes in any other way (such as the user changes the text).

The textEdited() signal also takes a QString as an argument and is emitted whenever text is edited (or changed). However if the text is changed via the setText() slot, then this signal is not emitted.

The one question that still remains about QLineEdit is that of how to access the text contained in QLineEdit. For this, the property of QLineEdit known as text() can be used. It returns a QString object containing the current text of the QLineEdit. That brings us to the next widget – QCheckBox.



 
 
>>> More Python Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: