我有3个文件:Gui.py,IPG_weld.py,Reading.py
Gui.py是:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMainWindow
from PyQt5.QtGui import QIcon
from PyQt5 import QtGui
from Ipg_weld import Ui_IPG_weld
class Test(Ui_IPG_weld):
def __init__(self):
super().__init__()
self.widget = QMainWindow()
self.setupUi(self.widget)
...
self.widget.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Test()
sys.exit(app.exec_())
Ipg_Weld.py是:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'D:\IPG_Weld\ipg_weld.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from Reading import reading
class Ui_IPG_weld(object):
def setupUi(self, IPG_weld):
IPG_weld.setObjectName("IPG_weld")
IPG_weld.resize(1107, 641)
self.centralWidget = QtWidgets.QWidget(IPG_weld)
self.centralWidget.setObjectName("centralWidget")
self.Savebutton = QtWidgets.QPushButton(self.centralWidget)
self.Savebutton.setGeometry(QtCore.QRect(930, 520, 131, 51))
self.Savebutton.setObjectName("Savebutton")
self.label = QtWidgets.QLabel(self.centralWidget)
self.label.setGeometry(QtCore.QRect(20, 20, 121, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralWidget)
self.label_2.setGeometry(QtCore.QRect(400, 20, 121, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralWidget)
self.label_3.setGeometry(QtCore.QRect(20, 80, 55, 16))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralWidget)
self.label_4.setGeometry(QtCore.QRect(20, 120, 55, 16))
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralWidget)
self.label_5.setGeometry(QtCore.QRect(20, 160, 55, 16))
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(self.centralWidget)
self.label_6.setGeometry(QtCore.QRect(400, 90, 55, 16))
self.label_6.setObjectName("label_6")
self.label_7 = QtWidgets.QLabel(self.centralWidget)
self.label_7.setGeometry(QtCore.QRect(400, 130, 55, 16))
self.label_7.setObjectName("label_7")
self.label_8 = QtWidgets.QLabel(self.centralWidget)
self.label_8.setGeometry(QtCore.QRect(400, 170, 55, 16))
self.label_8.setObjectName("label_8")
self.label_9 = QtWidgets.QLabel(self.centralWidget)
self.label_9.setGeometry(QtCore.QRect(400, 210, 55, 16))
self.label_9.setObjectName("label_9")
self.label_10 = QtWidgets.QLabel(self.centralWidget)
self.label_10.setGeometry(QtCore.QRect(400, 250, 55, 16))
self.label_10.setObjectName("label_10")
self.label_11 = QtWidgets.QLabel(self.centralWidget)
self.label_11.setGeometry(QtCore.QRect(400, 290, 55, 16))
self.label_11.setObjectName("label_11")
self.label_12 = QtWidgets.QLabel(self.centralWidget)
self.label_12.setGeometry(QtCore.QRect(400, 330, 55, 16))
self.label_12.setObjectName("label_12")
self.label_13 = QtWidgets.QLabel(self.centralWidget)
self.label_13.setGeometry(QtCore.QRect(400, 370, 55, 16))
self.label_13.setObjectName("label_13")
self.label_14 = QtWidgets.QLabel(self.centralWidget)
self.label_14.setGeometry(QtCore.QRect(400, 410, 55, 16))
self.label_14.setObjectName("label_14")
self.label_15 = QtWidgets.QLabel(self.centralWidget)
self.label_15.setGeometry(QtCore.QRect(400, 450, 55, 16))
self.label_15.setObjectName("label_15")
self.label_16 = QtWidgets.QLabel(self.centralWidget)
self.label_16.setGeometry(QtCore.QRect(400, 490, 55, 16))
self.label_16.setObjectName("label_16")
self.Read_Button = QtWidgets.QPushButton(self.centralWidget)
self.Read_Button.setGeometry(QtCore.QRect(790, 520, 131, 51))
self.Read_Button.setObjectName("Read_Button")
first_text=self.Read_Button.Clicked.Connect(reading())
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit.setGeometry(QtCore.QRect(220, 70, 41, 31))
self.plainTextEdit.setObjectName("plainTextEdit")
self.plainTextEdit_2 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_2.setGeometry(QtCore.QRect(220, 110, 41, 31))
self.plainTextEdit_2.setObjectName("plainTextEdit_2")
self.plainTextEdit_3 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_3.setGeometry(QtCore.QRect(220, 150, 41, 31))
self.plainTextEdit_3.setObjectName("plainTextEdit_3")
self.plainTextEdit_4 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_4.setGeometry(QtCore.QRect(620, 80, 41, 31))
self.plainTextEdit_4.setObjectName("plainTextEdit_4")
self.plainTextEdit_5 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_5.setGeometry(QtCore.QRect(620, 120, 41, 31))
self.plainTextEdit_5.setObjectName("plainTextEdit_5")
self.plainTextEdit_6 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_6.setGeometry(QtCore.QRect(620, 160, 41, 31))
self.plainTextEdit_6.setObjectName("plainTextEdit_6")
self.plainTextEdit_7 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_7.setGeometry(QtCore.QRect(620, 200, 41, 31))
self.plainTextEdit_7.setObjectName("plainTextEdit_7")
self.plainTextEdit_8 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_8.setGeometry(QtCore.QRect(620, 240, 41, 31))
self.plainTextEdit_8.setObjectName("plainTextEdit_8")
self.plainTextEdit_9 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_9.setGeometry(QtCore.QRect(620, 280, 41, 31))
self.plainTextEdit_9.setObjectName("plainTextEdit_9")
self.plainTextEdit_10 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_10.setGeometry(QtCore.QRect(620, 320, 41, 31))
self.plainTextEdit_10.setObjectName("plainTextEdit_10")
self.plainTextEdit_11 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_11.setGeometry(QtCore.QRect(620, 360, 41, 31))
self.plainTextEdit_11.setObjectName("plainTextEdit_11")
self.plainTextEdit_12 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_12.setGeometry(QtCore.QRect(620, 400, 41, 31))
self.plainTextEdit_12.setObjectName("plainTextEdit_12")
self.plainTextEdit_13 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_13.setGeometry(QtCore.QRect(620, 440, 41, 31))
self.plainTextEdit_13.setObjectName("plainTextEdit_13")
self.plainTextEdit_14 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_14.setGeometry(QtCore.QRect(620, 480, 41, 31))
self.plainTextEdit_14.setObjectName("plainTextEdit_14")
self.plainTextEdit_15 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_15.setGeometry(QtCore.QRect(160, 70, 41, 31))
self.plainTextEdit_15.setReadOnly(True)
self.plainTextEdit_15.setObjectName("plainTextEdit_15")
self.plainTextEdit_16 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_16.setGeometry(QtCore.QRect(160, 110, 41, 31))
self.plainTextEdit_16.setReadOnly(True)
self.plainTextEdit_16.setObjectName("plainTextEdit_16")
self.plainTextEdit_17 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_17.setGeometry(QtCore.QRect(160, 150, 41, 31))
self.plainTextEdit_17.setReadOnly(True)
self.plainTextEdit_17.setObjectName("plainTextEdit_17")
self.plainTextEdit_18 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_18.setGeometry(QtCore.QRect(560, 80, 41, 31))
self.plainTextEdit_18.setReadOnly(True)
self.plainTextEdit_18.setObjectName("plainTextEdit_18")
self.plainTextEdit_19 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_19.setGeometry(QtCore.QRect(560, 120, 41, 31))
self.plainTextEdit_19.setReadOnly(True)
self.plainTextEdit_19.setObjectName("plainTextEdit_19")
self.plainTextEdit_20 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_20.setGeometry(QtCore.QRect(560, 160, 41, 31))
self.plainTextEdit_20.setReadOnly(True)
self.plainTextEdit_20.setObjectName("plainTextEdit_20")
self.plainTextEdit_21 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_21.setGeometry(QtCore.QRect(560, 200, 41, 31))
self.plainTextEdit_21.setReadOnly(True)
self.plainTextEdit_21.setObjectName("plainTextEdit_21")
self.plainTextEdit_22 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_22.setGeometry(QtCore.QRect(560, 240, 41, 31))
self.plainTextEdit_22.setReadOnly(True)
self.plainTextEdit_22.setObjectName("plainTextEdit_22")
self.plainTextEdit_23 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_23.setGeometry(QtCore.QRect(560, 280, 41, 31))
self.plainTextEdit_23.setReadOnly(True)
self.plainTextEdit_23.setObjectName("plainTextEdit_23")
self.plainTextEdit_24 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_24.setGeometry(QtCore.QRect(560, 320, 41, 31))
self.plainTextEdit_24.setReadOnly(True)
self.plainTextEdit_24.setObjectName("plainTextEdit_24")
self.plainTextEdit_25 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_25.setGeometry(QtCore.QRect(560, 360, 41, 31))
self.plainTextEdit_25.setReadOnly(True)
self.plainTextEdit_25.setObjectName("plainTextEdit_25")
self.plainTextEdit_26 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_26.setGeometry(QtCore.QRect(560, 400, 41, 31))
self.plainTextEdit_26.setReadOnly(True)
self.plainTextEdit_26.setObjectName("plainTextEdit_26")
self.plainTextEdit_27 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_27.setGeometry(QtCore.QRect(560, 440, 41, 31))
self.plainTextEdit_27.setReadOnly(True)
self.plainTextEdit_27.setObjectName("plainTextEdit_27")
self.plainTextEdit_28 = QtWidgets.QPlainTextEdit(self.centralWidget)
self.plainTextEdit_28.setGeometry(QtCore.QRect(560, 480, 41, 31))
self.plainTextEdit_28.setReadOnly(True)
self.plainTextEdit_28.setObjectName("plainTextEdit_28")
self.label_17 = QtWidgets.QLabel(self.centralWidget)
self.label_17.setGeometry(QtCore.QRect(150, 40, 55, 16))
self.label_17.setObjectName("label_17")
self.label_18 = QtWidgets.QLabel(self.centralWidget)
self.label_18.setGeometry(QtCore.QRect(220, 40, 55, 16))
self.label_18.setObjectName("label_18")
self.label_19 = QtWidgets.QLabel(self.centralWidget)
self.label_19.setGeometry(QtCore.QRect(550, 50, 55, 16))
self.label_19.setObjectName("label_19")
self.label_20 = QtWidgets.QLabel(self.centralWidget)
self.label_20.setGeometry(QtCore.QRect(620, 50, 55, 16))
self.label_20.setObjectName("label_20")
IPG_weld.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(IPG_weld)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 1107, 26))
self.menuBar.setObjectName("menuBar")
IPG_weld.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(IPG_weld)
self.mainToolBar.setObjectName("mainToolBar")
IPG_weld.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(IPG_weld)
self.statusBar.setObjectName("statusBar")
IPG_weld.setStatusBar(self.statusBar)
self.retranslateUi(IPG_weld)
QtCore.QMetaObject.connectSlotsByName(IPG_weld)
def retranslateUi(self, IPG_weld):
_translate = QtCore.QCoreApplication.translate
IPG_weld.setWindowTitle(_translate("IPG_weld", "IPG_weld"))
self.Savebutton.setText(_translate("IPG_weld", "Сохранить"))
self.label.setText(_translate("IPG_weld", "Inputs"))
self.label_2.setText(_translate("IPG_weld", "Outputs"))
self.label_3.setText(_translate("IPG_weld", "Active"))
self.label_4.setText(_translate("IPG_weld", "Ready"))
self.label_5.setText(_translate("IPG_weld", "Error"))
self.label_6.setText(_translate("IPG_weld", "Bit 0"))
self.label_7.setText(_translate("IPG_weld", "Bit 1"))
self.label_8.setText(_translate("IPG_weld", "Bit 2"))
self.label_9.setText(_translate("IPG_weld", "Bit 3"))
self.label_10.setText(_translate("IPG_weld", "Bit 4"))
self.label_11.setText(_translate("IPG_weld", "Bit 5"))
self.label_12.setText(_translate("IPG_weld", "Bit 6"))
self.label_13.setText(_translate("IPG_weld", "Bit 7"))
self.label_14.setText(_translate("IPG_weld", "Bit 8"))
self.label_15.setText(_translate("IPG_weld", "Strobe"))
self.label_16.setText(_translate("IPG_weld", "Start"))
self.Read_Button.setText(_translate("IPG_weld", "Считать значения"))
#self.plainTextEdit_18.setPlainText(_translate("IPG_weld", "1"))
self.plainTextEdit_18.setPlainText(first_edit[1])
self.plainTextEdit_19.setPlainText(_translate("IPG_weld", "2"))
self.plainTextEdit_20.setPlainText(_translate("IPG_weld", "3"))
self.plainTextEdit_21.setPlainText(_translate("IPG_weld", "4"))
self.plainTextEdit_22.setPlainText(_translate("IPG_weld", "5"))
self.plainTextEdit_23.setPlainText(_translate("IPG_weld", "6"))
self.plainTextEdit_24.setPlainText(_translate("IPG_weld", "7"))
self.plainTextEdit_25.setPlainText(_translate("IPG_weld", "8"))
self.plainTextEdit_26.setPlainText(_translate("IPG_weld", "9"))
self.plainTextEdit_27.setPlainText(_translate("IPG_weld", "10"))
self.plainTextEdit_28.setPlainText(_translate("IPG_weld", "11"))
self.label_17.setText(_translate("IPG_weld", "Текущее "))
self.label_18.setText(_translate("IPG_weld", "Новое"))
self.label_19.setText(_translate("IPG_weld", "Текущее "))
self.label_20.setText(_translate("IPG_weld", "Новое"))
而Reading.py是:
import sys
import os
import re
import re
def reading():
with open ('Setup_Weld.src') as f:
stripped=[row.strip() for row in f]
setup_weld_num = [re.search(r'\[(.*?)\]',i).group(1) for i in stripped if re.search(r'\[(.*?)\]',i)]
print(setup_weld_num)
reading()
所以我当前要做的就是将在IPG_weld.py(Read_Button和Save_Button)中创建的两个按钮连接到我的事件: 当单击Read_Button时,应该执行reading.py中的reading(),其中将填充数组Setup_weld_num,并且 PlainTextEdit_18到PlainTextEdit_28接收来自setup_weld_num数组的元素。换句话说,我想从文件中读取:
&ACCESS RVP1
&REL 3
&PARAM DISKPATH = KRC:\R1\Program\new
DEF SETUP_WELD()
;call LASER_EN();
$out[27]=false; START=OFF
$out[26]=false; STROBE=OFF
$out[25]=false; SELECT 8=OFF
$out[24]=false; SELECT 7 =OFF
$out[23]=false; SELECT 6 =OFF
$out[22]=false; SELECT 5 =OFF
$out[21]=false; SELECT 4 =OFF
$out[20]=false; SELECT 3 =OFF
$out[19]=false; SELECT 2 =OFF
$out[18]=false; SELECT 1 =OFF
$out[17]=false; SELECT 0 =OFF
$out[28]=true; ENABLE=ON
END
将输出数转换为Plain_TextEdit_18 - plainTextEdit_25,这样用户就可以在Gui的文本框中看到它们。
怎么做得好?
UPD:你可以看到我添加了:
first_text=self.Read_Button.Clicked.Connect(reading())
并尝试将 first_edit 数组的第一个元素转移到 PlainTextEdit_18 :
self.plainTextEdit_18.setPlainText(first_edit[1])
但它不起作用,因为对于retranslate_UI函数,数组 first_edit 不可见。
答案 0 :(得分:1)
作为对Ipg_weld.py
个州顶部的评论,将任何代码添加到此文件是没用的,因为它将由Qt UI编译器重写。考虑在Test
中向您的班级Gui.py
添加点击处理函数。例如:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMainWindow
from PyQt5.QtGui import QIcon
from PyQt5 import QtGui
from Ipg_weld import Ui_IPG_weld
import Reading
class Test(Ui_IPG_weld):
def __init__(self):
super().__init__()
self.widget = QMainWindow()
self.setupUi(self.widget)
# ...
self.widget.show()
def read_file(self):
file_values = Reading.reading()
print("Got values:", file_values)
# change this accordingly
self.plainTextEdit.setPlainText(file_values[0])
self.plainTextEdit_2.setPlainText(file_values[1])
# ...
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Test()
sys.exit(app.exec_())
然后你有处理函数,连接按钮信号。请注意,QPushButton
信号clicked(大小写很重要),信号方法connect
(more about signals and slots in pyqt)。
在您的示例行first_text=self.Read_Button.Clicked.Connect(reading())
实际调用 reading
(请注意括号),但在连接信号和插槽时,您必须提供可调用的connect
方法(考虑它就像你指定一个函数名而不是调用它)。
总结一下,添加与课程<{>> __init__
的连接:
class Test(Ui_IPG_weld):
def __init__(self):
super().__init__()
self.widget = QMainWindow()
self.setupUi(self.widget)
# ...
# connecting button. Note that you should not call
# self.read_file here.
self.Read_Button.clicked.connect(self.read_file)
self.widget.show()
现在,您应该在点击按钮时看到file_values
打印。