鱼C论坛

 找回密码
 立即注册
查看: 1663|回复: 3

[已解决]如何获取输入框的值?

[复制链接]
发表于 2020-12-5 21:20:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 wnhuiyou 于 2020-12-5 22:10 编辑

通过调用类我可以创建多个按钮,这个我已经实现了,也能达到我想要的效果,就是每个按钮可以出发不同的函数,但是文本输入框,我一样的操作,也能创建多个,但是如何获取到不同文本框里面的值?因为现在我写的,文本输入框的值都是一样的。

  1. import sys
  2. from PyQt5.Qt import QWidget, QApplication
  3. from PyQt5.QtWidgets import QPushButton, QLabel, QLineEdit


  4. class YC(QWidget):

  5.     def __init__(self):
  6.         super().__init__()

  7.         # 输入框
  8.         self.ipaddr = self.LEdit('', 125, 200)  # 外网IP
  9.         self.mask = self.LEdit('', 125, 240)  # 子网掩码
  10.         self.gateway = self.LEdit('', 125, 280)  # 外网网关
  11.         self.GAgateway = self.LEdit('192.168.1.1', 125, 320)  # 公安网关

  12.         # 标题
  13.         self.QLabel("外网IP:", "font:20px", 50, 205)
  14.         self.QLabel("子网掩码:", "font:20px", 30, 245)
  15.         self.QLabel("外网网关:", "font:20px", 30, 285)
  16.         self.QLabel("公安网关:", "font:20px", 30, 325)

  17.         # 按钮
  18.         self.QPushButton("设置路由表", "font:15px", 90, 30, 55, 410)
  19.         self.QPushButton("删除路由表", "font:15px", 90, 30, 170, 410)
  20.         self.QPushButton("测试公安", "font:15px", 90, 30, 170, 450)
  21.         self.QPushButton("测试外网", "font:15px", 90, 30, 55, 450)
  22.         self.QPushButton("查看路由", "font:15px", 75, 30, 185, 370)
  23.         self.QPushButton("外网IP", "font:15px", 60, 30, 55, 370)
  24.         self.QPushButton("公安IP", "font:15px", 60, 30, 120, 370)

  25.     # 标题
  26.     def QLabel(self, Text, StyleSheet, move_x, move_y):
  27.         label = QLabel(self)
  28.         label.setText(Text)
  29.         label.setStyleSheet(StyleSheet)
  30.         label.move(move_x, move_y)

  31.     # 按钮
  32.     def QPushButton(self, Text, StyleSheet, resize_x, resize_y, move_x, move_y):
  33.         btn = QPushButton(self)
  34.         btn.setText(Text)
  35.         btn.setStyleSheet(StyleSheet)
  36.         btn.resize(resize_x, resize_y)
  37.         btn.move(move_x, move_y)
  38.         btn.clicked.connect(self.SetIP)

  39.     # 输入框
  40.     def LEdit(self, setText, move_x, move_y):
  41.         Input = QLineEdit(self)
  42.         Input.setText(setText)
  43.         print(setText)
  44.         Input.resize(155, 30)
  45.         Input.move(move_x, move_y)
  46.         Input.setStyleSheet("font:20px")
  47.         return Input.text()

  48.     def SetIP(self):
  49.         command = '"netsh interface ipv4 set address "本地连接" static %s %s %s"' % (self.ipaddr, self.mask, self.GAgateway)
  50.         # os.system(command)
  51.         print(command)


  52. if __name__ == '__main__':
  53.     app = QApplication(sys.argv)
  54.     win = YC()
  55.     win.setWindowTitle("小软件")
  56.     win.setFixedSize(300, 500)  # 设定固定窗口大小

  57.     win.show()
  58.     sys.exit(app.exec_())
复制代码


QQ浏览器截图20201205212009.png


何如获取每个不同输入款里面的值
最佳答案
2020-12-6 10:17:43
  1. import sys
  2. from PyQt5.Qt import QWidget, QApplication
  3. from PyQt5.QtWidgets import QPushButton, QLabel, QLineEdit


  4. class YC(QWidget):

  5.     def __init__(self):
  6.         super().__init__()

  7.         # 输入框
  8.         self.ipaddr = self.LEdit('', 125, 200)  # 外网IP
  9.         self.mask = self.LEdit('', 125, 240)  # 子网掩码
  10.         self.gateway = self.LEdit('', 125, 280)  # 外网网关
  11.         self.GAgateway = self.LEdit('192.168.1.1', 125, 320)  # 公安网关

  12.         # 标题
  13.         self.QLabel("外网IP:", "font:20px", 50, 205)
  14.         self.QLabel("子网掩码:", "font:20px", 30, 245)
  15.         self.QLabel("外网网关:", "font:20px", 30, 285)
  16.         self.QLabel("公安网关:", "font:20px", 30, 325)

  17.         # 按钮
  18.         self.QPushButton("设置路由表", "font:15px", 90, 30, 55, 410)
  19.         self.QPushButton("删除路由表", "font:15px", 90, 30, 170, 410)
  20.         self.QPushButton("测试公安", "font:15px", 90, 30, 170, 450)
  21.         self.QPushButton("测试外网", "font:15px", 90, 30, 55, 450)
  22.         self.QPushButton("查看路由", "font:15px", 75, 30, 185, 370)
  23.         self.QPushButton("外网IP", "font:15px", 60, 30, 55, 370)
  24.         self.QPushButton("公安IP", "font:15px", 60, 30, 120, 370)

  25.     # 标题
  26.     def QLabel(self, Text, StyleSheet, move_x, move_y):
  27.         label = QLabel(self)
  28.         label.setText(Text)
  29.         label.setStyleSheet(StyleSheet)
  30.         label.move(move_x, move_y)

  31.     # 按钮
  32.     def QPushButton(self, Text, StyleSheet, resize_x, resize_y, move_x, move_y):
  33.         btn = QPushButton(self)
  34.         btn.setText(Text)
  35.         btn.setStyleSheet(StyleSheet)
  36.         btn.resize(resize_x, resize_y)
  37.         btn.move(move_x, move_y)
  38.         btn.clicked.connect(self.SetIP)

  39.     # 输入框
  40.     def LEdit(self, setText, move_x, move_y):
  41.         Input = QLineEdit(self)
  42.         Input.setText(setText)
  43.         print(setText)
  44.         Input.resize(155, 30)
  45.         Input.move(move_x, move_y)
  46.         Input.setStyleSheet("font:20px")
  47.         return Input

  48.     def SetIP(self):
  49.         command = '"netsh interface ipv4 set address "本地连接" static %s %s %s"' % (self.ipaddr.text(), self.mask.text(), self.GAgateway.text())
  50.         # os.system(command)
  51.         print(command)


  52. if __name__ == '__main__':
  53.     app = QApplication(sys.argv)
  54.     win = YC()
  55.     win.setWindowTitle("小软件")
  56.     win.setFixedSize(300, 500)  # 设定固定窗口大小

  57.     win.show()
  58.     sys.exit(app.exec_())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-5 21:23:58 | 显示全部楼层
https://blog.csdn.net/jia666666/article/details/81510502

QLineEdit.text() 就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-5 21:54:18 | 显示全部楼层
本帖最后由 wnhuiyou 于 2020-12-5 21:59 编辑
笨鸟学飞 发表于 2020-12-5 21:23
https://blog.csdn.net/jia666666/article/details/81510502

QLineEdit.text() 就行了


我知道用 QLineEdit.text()  我 54行 就返回了值,但是重新输入值后,获取不到
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-6 10:17:43 | 显示全部楼层    本楼为最佳答案   
  1. import sys
  2. from PyQt5.Qt import QWidget, QApplication
  3. from PyQt5.QtWidgets import QPushButton, QLabel, QLineEdit


  4. class YC(QWidget):

  5.     def __init__(self):
  6.         super().__init__()

  7.         # 输入框
  8.         self.ipaddr = self.LEdit('', 125, 200)  # 外网IP
  9.         self.mask = self.LEdit('', 125, 240)  # 子网掩码
  10.         self.gateway = self.LEdit('', 125, 280)  # 外网网关
  11.         self.GAgateway = self.LEdit('192.168.1.1', 125, 320)  # 公安网关

  12.         # 标题
  13.         self.QLabel("外网IP:", "font:20px", 50, 205)
  14.         self.QLabel("子网掩码:", "font:20px", 30, 245)
  15.         self.QLabel("外网网关:", "font:20px", 30, 285)
  16.         self.QLabel("公安网关:", "font:20px", 30, 325)

  17.         # 按钮
  18.         self.QPushButton("设置路由表", "font:15px", 90, 30, 55, 410)
  19.         self.QPushButton("删除路由表", "font:15px", 90, 30, 170, 410)
  20.         self.QPushButton("测试公安", "font:15px", 90, 30, 170, 450)
  21.         self.QPushButton("测试外网", "font:15px", 90, 30, 55, 450)
  22.         self.QPushButton("查看路由", "font:15px", 75, 30, 185, 370)
  23.         self.QPushButton("外网IP", "font:15px", 60, 30, 55, 370)
  24.         self.QPushButton("公安IP", "font:15px", 60, 30, 120, 370)

  25.     # 标题
  26.     def QLabel(self, Text, StyleSheet, move_x, move_y):
  27.         label = QLabel(self)
  28.         label.setText(Text)
  29.         label.setStyleSheet(StyleSheet)
  30.         label.move(move_x, move_y)

  31.     # 按钮
  32.     def QPushButton(self, Text, StyleSheet, resize_x, resize_y, move_x, move_y):
  33.         btn = QPushButton(self)
  34.         btn.setText(Text)
  35.         btn.setStyleSheet(StyleSheet)
  36.         btn.resize(resize_x, resize_y)
  37.         btn.move(move_x, move_y)
  38.         btn.clicked.connect(self.SetIP)

  39.     # 输入框
  40.     def LEdit(self, setText, move_x, move_y):
  41.         Input = QLineEdit(self)
  42.         Input.setText(setText)
  43.         print(setText)
  44.         Input.resize(155, 30)
  45.         Input.move(move_x, move_y)
  46.         Input.setStyleSheet("font:20px")
  47.         return Input

  48.     def SetIP(self):
  49.         command = '"netsh interface ipv4 set address "本地连接" static %s %s %s"' % (self.ipaddr.text(), self.mask.text(), self.GAgateway.text())
  50.         # os.system(command)
  51.         print(command)


  52. if __name__ == '__main__':
  53.     app = QApplication(sys.argv)
  54.     win = YC()
  55.     win.setWindowTitle("小软件")
  56.     win.setFixedSize(300, 500)  # 设定固定窗口大小

  57.     win.show()
  58.     sys.exit(app.exec_())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-30 19:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表