如何在窗口上显示 QLineEdit? [英] How to display QLineEdit on the window?

查看:59
本文介绍了如何在窗口上显示 QLineEdit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PyQt4 和 Pydev 创建了一个小窗口.代码如下:

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_())

我创建了 Password LineEdit 框,但如何在活动窗口中显示,由 widget 表示?

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

推荐答案

就用

Password = QtGui.QLineEdit(widget)

这告诉 Qt 您希望 widget 成为 QLineEdit 的父级.如果省略 widget,则 QLineEdit 没有父级,因此不会显示.

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.

更新:要在父窗口中定位子项,您必须阅读有关布局的信息(我假设您想正确地进行布局,而不是作为玩具/学习练习).任何好的 PyQt 书籍都应该能够提供帮助,例如这个.

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, e.g. this one.

这篇关于如何在窗口上显示 QLineEdit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆