Skip to content

Files

Latest commit

f9ba77b · Nov 22, 2019

History

History
79 lines (52 loc) · 1.58 KB

219.md

File metadata and controls

79 lines (52 loc) · 1.58 KB

PyQt4 文本框

原文: https://pythonspot.com/qt4-textbox-example/

pyqt textbox

PyQt4 文本框示例

在本文中,您将学习如何使用 PyQt4 与文本框进行交互。

如果要在文本框(QLineEdit)中显示文本,则可以使用setText()方法。

PyQt4 QLineEdit

如果按下按钮,下面的文本框示例将更改文本。

import sys
from PyQt4.QtCore import pyqtSlot
from PyQt4.QtGui import *

# create our window
app = QApplication(sys.argv)
w = QWidget()
w.setWindowTitle('Textbox example @pythonspot.com')

# Create textbox
textbox = QLineEdit(w)
textbox.move(20, 20)
textbox.resize(280,40)

# Set window size.
w.resize(320, 150)

# Create a button in the window
button = QPushButton('Click me', w)
button.move(20,80)

# Create the actions
@pyqtSlot()
def on_click():
    textbox.setText("Button clicked.")

# connect the signals to the slots
button.clicked.connect(on_click)

# Show the window and run the app
w.show()
app.exec_()

使用以下行创建文本字段:

textbox = QLineEdit(w)
textbox.move(20, 20)
textbox.resize(280,40)

该按钮(来自屏幕截图)由以下部分制成:

button = QPushButton('Click me', w)

我们通过以下方式将按钮连接到on_click函数:

# connect the signals to the slots
button.clicked.connect(on_click)

此函数使用setText()设置文本框。

下载 PyQT 代码(批量收集)