|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 wnhuiyou 于 2020-12-5 22:10 编辑
通过调用类我可以创建多个按钮,这个我已经实现了,也能达到我想要的效果,就是每个按钮可以出发不同的函数,但是文本输入框,我一样的操作,也能创建多个,但是如何获取到不同文本框里面的值?因为现在我写的,文本输入框的值都是一样的。
import sys
from PyQt5.Qt import QWidget, QApplication
from PyQt5.QtWidgets import QPushButton, QLabel, QLineEdit
class YC(QWidget):
def __init__(self):
super().__init__()
# 输入框
self.ipaddr = self.LEdit('', 125, 200) # 外网IP
self.mask = self.LEdit('', 125, 240) # 子网掩码
self.gateway = self.LEdit('', 125, 280) # 外网网关
self.GAgateway = self.LEdit('192.168.1.1', 125, 320) # 公安网关
# 标题
self.QLabel("外网IP:", "font:20px", 50, 205)
self.QLabel("子网掩码:", "font:20px", 30, 245)
self.QLabel("外网网关:", "font:20px", 30, 285)
self.QLabel("公安网关:", "font:20px", 30, 325)
# 按钮
self.QPushButton("设置路由表", "font:15px", 90, 30, 55, 410)
self.QPushButton("删除路由表", "font:15px", 90, 30, 170, 410)
self.QPushButton("测试公安", "font:15px", 90, 30, 170, 450)
self.QPushButton("测试外网", "font:15px", 90, 30, 55, 450)
self.QPushButton("查看路由", "font:15px", 75, 30, 185, 370)
self.QPushButton("外网IP", "font:15px", 60, 30, 55, 370)
self.QPushButton("公安IP", "font:15px", 60, 30, 120, 370)
# 标题
def QLabel(self, Text, StyleSheet, move_x, move_y):
label = QLabel(self)
label.setText(Text)
label.setStyleSheet(StyleSheet)
label.move(move_x, move_y)
# 按钮
def QPushButton(self, Text, StyleSheet, resize_x, resize_y, move_x, move_y):
btn = QPushButton(self)
btn.setText(Text)
btn.setStyleSheet(StyleSheet)
btn.resize(resize_x, resize_y)
btn.move(move_x, move_y)
btn.clicked.connect(self.SetIP)
# 输入框
def LEdit(self, setText, move_x, move_y):
Input = QLineEdit(self)
Input.setText(setText)
print(setText)
Input.resize(155, 30)
Input.move(move_x, move_y)
Input.setStyleSheet("font:20px")
return Input.text()
def SetIP(self):
command = '"netsh interface ipv4 set address "本地连接" static %s %s %s"' % (self.ipaddr, self.mask, self.GAgateway)
# os.system(command)
print(command)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = YC()
win.setWindowTitle("小软件")
win.setFixedSize(300, 500) # 设定固定窗口大小
win.show()
sys.exit(app.exec_())
何如获取每个不同输入款里面的值
import sys
from PyQt5.Qt import QWidget, QApplication
from PyQt5.QtWidgets import QPushButton, QLabel, QLineEdit
class YC(QWidget):
def __init__(self):
super().__init__()
# 输入框
self.ipaddr = self.LEdit('', 125, 200) # 外网IP
self.mask = self.LEdit('', 125, 240) # 子网掩码
self.gateway = self.LEdit('', 125, 280) # 外网网关
self.GAgateway = self.LEdit('192.168.1.1', 125, 320) # 公安网关
# 标题
self.QLabel("外网IP:", "font:20px", 50, 205)
self.QLabel("子网掩码:", "font:20px", 30, 245)
self.QLabel("外网网关:", "font:20px", 30, 285)
self.QLabel("公安网关:", "font:20px", 30, 325)
# 按钮
self.QPushButton("设置路由表", "font:15px", 90, 30, 55, 410)
self.QPushButton("删除路由表", "font:15px", 90, 30, 170, 410)
self.QPushButton("测试公安", "font:15px", 90, 30, 170, 450)
self.QPushButton("测试外网", "font:15px", 90, 30, 55, 450)
self.QPushButton("查看路由", "font:15px", 75, 30, 185, 370)
self.QPushButton("外网IP", "font:15px", 60, 30, 55, 370)
self.QPushButton("公安IP", "font:15px", 60, 30, 120, 370)
# 标题
def QLabel(self, Text, StyleSheet, move_x, move_y):
label = QLabel(self)
label.setText(Text)
label.setStyleSheet(StyleSheet)
label.move(move_x, move_y)
# 按钮
def QPushButton(self, Text, StyleSheet, resize_x, resize_y, move_x, move_y):
btn = QPushButton(self)
btn.setText(Text)
btn.setStyleSheet(StyleSheet)
btn.resize(resize_x, resize_y)
btn.move(move_x, move_y)
btn.clicked.connect(self.SetIP)
# 输入框
def LEdit(self, setText, move_x, move_y):
Input = QLineEdit(self)
Input.setText(setText)
print(setText)
Input.resize(155, 30)
Input.move(move_x, move_y)
Input.setStyleSheet("font:20px")
return Input
def SetIP(self):
command = '"netsh interface ipv4 set address "本地连接" static %s %s %s"' % (self.ipaddr.text(), self.mask.text(), self.GAgateway.text())
# os.system(command)
print(command)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = YC()
win.setWindowTitle("小软件")
win.setFixedSize(300, 500) # 设定固定窗口大小
win.show()
sys.exit(app.exec_())
|
|