简体   繁体   中英

How to display QLineEdit on the window?

I created a small window using PyQt4 and Pydev. The code is below:

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

# Create GUI object
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()

widget.setGeometry(400,300,800,800) # Position window
widget.resize(450,250)  # Resize window

widget.setWindowTitle('Sample')   # Set Title of the window

Password = QtGui.QLineEdit()    # Input Box for password

widget.show()   # Display window

# Exit program
sys.exit(app.exec_())

I created the Password LineEdit box but how to show on the active window, which is represented by widget ?

Just use

Password = QtGui.QLineEdit(widget)

This tells Qt that you want widget to be the parent of the QLineEdit . If you leave out the widget , then the QLineEdit has no parent, so it's not shown.

Update: To position child items in parent windows, you'll have to read up about layouts (I assume you want to do it properly, not as a toy/learning exercise). Any good PyQt book should be able to help, eg this one .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM