pyqt5获取输入框内容_PyQt5系列教程(8):标准输入对话框

上期我们介绍了PyQt5软件界面的搭建,主要涉及到了以下几个知识点:

状态栏

菜单

工具栏

今天我们主要讲解一下标准输入对话框的知识。

对话框窗口或对话框是大多数现代GUI应用程序中不可或缺的一部分。在计算机应用程序中,一个对话框是一个用于“与应用程序”通话的窗口。对话框用于输入数据,修改数据,更改应用程序设置等。因为对话框涉及的内容比较多,我们计划分成几个章节来讲解,今天主要介绍输入对话框。

QInputDialog提供了一个简单的便利对话框,可以从用户获取单个值。 输入值可以是列表中的字符串,数字或项目。我们会分别通过一个例子来看看一下。效果如下:

首先看下QInputDialog包含的几个方法:

getDouble(QWidget, str, str, value: float = 0, min: float = -2147483647, max: float = 2147483647, decimals: int = 1, flags: Union[Qt.WindowFlags,Qt.WindowType]=Qt.WindowFlags())->Tuple[float,bool]

getInt(QWidget, str, str, value: int = 0, min: int = -2147483647, max: int = 2147483647, step: int = 1, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()) -> Tuple[int, bool]

getItem(QWidget, str, str, Iterable[str], current:int=0,editable:bool=True,flags:Union[Qt.WindowFlags,Qt.WindowType]=Qt.WindowFlags(),inputMethodHints:Union[Qt.InputMethodHints,Qt.InputMethodHint] = Qt.ImhNone) -> Tuple[str, bool]

getMultiLineText(QWidget, str, str, text: str = '', flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags(), inputMethodHints: Union[Qt.InputMethodH

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用PyQt5中的QLineEdit小部件来获取输入框内容,并将其存储到MySQL数据库中。下面是一个简单的示例代码: ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit, QPushButton import mysql.connector class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("存储到MySQL") self.setGeometry(300, 200, 400, 300) self.label = QLabel(self) self.label.setText("请输入内容:") self.label.setGeometry(50, 50, 100, 30) self.input_box = QLineEdit(self) self.input_box.setGeometry(150, 50, 200, 30) self.save_button = QPushButton(self) self.save_button.setText("保存") self.save_button.setGeometry(150, 100, 100, 30) self.save_button.clicked.connect(self.save_to_mysql) def save_to_mysql(self): content = self.input_box.text() # 连接到MySQL数据库 cnx = mysql.connector.connect(user='your_username', password='your_password', host='localhost', database='your_database') cursor = cnx.cursor() # 在表中插入数据 add_data = ("INSERT INTO your_table (content) VALUES (%s)") data = (content,) cursor.execute(add_data, data) # 提交更改并关闭连接 cnx.commit() cursor.close() cnx.close() # 清空输入框 self.input_box.clear() if __name__ == "__main__": import sys app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` 请注意,您需要将代码中的`your_username`,`your_password`,`localhost`,`your_database`和`your_table`替换为您自己的MySQL数据库的信息。此外,您需要确保已安装`mysql-connector-python`库,您可以使用以下命令安装它: ``` pip install mysql-connector-python ``` 此示例代码创建了一个带有标签、输入框和保存按钮的窗口。当用户点击保存按钮时,它将获取输入框内容并将其插入到MySQL数据库中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值