我对python和Qt还是很陌生,距我开始学习python仅两周了。我正在做一个个人项目,机器制造处于最后阶段。这是3D绘图仪。目前,我正在手动发送gcode。我一直盯着使用python,Qt和pyQt开发一个简单的GUI。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'GUIv0.1.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(869, 927)
self.XYcoordinates_tbox = QtWidgets.QTextEdit(Form)
self.XYcoordinates_tbox.setGeometry(QtCore.QRect(50, 50, 241, 301))
self.XYcoordinates_tbox.setObjectName("XYcoordinates_tbox")
self.XYcoordinates_label = QtWidgets.QLabel(Form)
self.XYcoordinates_label.setGeometry(QtCore.QRect(50, 20, 231, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.XYcoordinates_label.setFont(font)
self.XYcoordinates_label.setObjectName("XYcoordinates_label")
self.Xcoordinate_tbox = QtWidgets.QTextEdit(Form)
self.Xcoordinate_tbox.setGeometry(QtCore.QRect(630, 60, 51, 41))
self.Xcoordinate_tbox.setObjectName("Xcoorbdinate_tbox")
self.Ycoordinate_tbox = QtWidgets.QTextEdit(Form)
self.Ycoordinate_tbox.setGeometry(QtCore.QRect(710, 60, 51, 41))
self.Ycoordinate_tbox.setObjectName("Ycoordinate_tbox")
self.X_label = QtWidgets.QLabel(Form)
self.X_label.setGeometry(QtCore.QRect(650, 30, 31, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.X_label.setFont(font)
self.X_label.setObjectName("X_label")
self.Y_label = QtWidgets.QLabel(Form)
self.Y_label.setGeometry(QtCore.QRect(730, 30, 31, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.Y_label.setFont(font)
self.Y_label.setObjectName("Y_label")
self.XYcoordinate_label = QtWidgets.QLabel(Form)
self.XYcoordinate_label.setGeometry(QtCore.QRect(500, 70, 111, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.XYcoordinate_label.setFont(font)
self.XYcoordinate_label.setObjectName("XYcoordinate_label")
self.XY_button = QtWidgets.QPushButton(Form)
self.XY_button.setGeometry(QtCore.QRect(780, 60, 61, 41))
font = QtGui.QFont()
font.setPointSize(10)
self.XY_button.setFont(font)
self.XY_button.setObjectName("XY_button")
self.XYspeed_label = QtWidgets.QLabel(Form)
self.XYspeed_label.setGeometry(QtCore.QRect(530, 140, 81, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.XYspeed_label.setFont(font)
self.XYspeed_label.setObjectName("XYspeed_label")
self.XYspeed_tbox = QtWidgets.QTextEdit(Form)
self.XYspeed_tbox.setGeometry(QtCore.QRect(630, 130, 131, 41))
self.XYspeed_tbox.setObjectName("XYspeed_tbox")
self.Gengoce_button = QtWidgets.QPushButton(Form)
self.Gengoce_button.setGeometry(QtCore.QRect(560, 830, 291, 71))
self.Gengoce_button.setObjectName("Gengoce_button")
self.Impdepth_tbox = QtWidgets.QTextEdit(Form)
self.Impdepth_tbox.setGeometry(QtCore.QRect(630, 190, 131, 41))
self.Impdepth_tbox.setObjectName("Impdepth_tbox")
self.Impdepth_label = QtWidgets.QLabel(Form)
self.Impdepth_label.setGeometry(QtCore.QRect(470, 200, 141, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.Impdepth_label.setFont(font)
self.Impdepth_label.setObjectName("Impdepth_label")
self.Displiq_tbox = QtWidgets.QTextEdit(Form)
self.Displiq_tbox.setGeometry(QtCore.QRect(630, 460, 131, 41))
self.Displiq_tbox.setObjectName("Displiq_tbox")
self.Displiq_label = QtWidgets.QLabel(Form)
self.Displiq_label.setGeometry(QtCore.QRect(470, 470, 141, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.Displiq_label.setFont(font)
self.Displiq_label.setObjectName("Displiq_label")
self.Impspeed_tbox = QtWidgets.QTextEdit(Form)
self.Impspeed_tbox.setGeometry(QtCore.QRect(630, 250, 131, 41))
self.Impspeed_tbox.setObjectName("Impspeed_tbox")
self.Impspeed_label = QtWidgets.QLabel(Form)
self.Impspeed_label.setGeometry(QtCore.QRect(440, 260, 171, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.Impspeed_label.setFont(font)
self.Impspeed_label.setObjectName("Impspeed_label")
self.Xoffset_tbox = QtWidgets.QTextEdit(Form)
self.Xoffset_tbox.setGeometry(QtCore.QRect(630, 320, 51, 41))
self.Xoffset_tbox.setObjectName("Xoffset_tbox")
self.Xoffset_label = QtWidgets.QLabel(Form)
self.Xoffset_label.setGeometry(QtCore.QRect(500, 330, 111, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.Xoffset_label.setFont(font)
self.Xoffset_label.setObjectName("Xoffset_label")
self.Yoffset_tbox = QtWidgets.QTextEdit(Form)
self.Yoffset_tbox.setGeometry(QtCore.QRect(710, 320, 51, 41))
self.Yoffset_tbox.setObjectName("Yoffset_tbox")
self.mlperrev_label = QtWidgets.QLabel(Form)
self.mlperrev_label.setGeometry(QtCore.QRect(480, 400, 131, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.mlperrev_label.setFont(font)
self.mlperrev_label.setObjectName("mlperrev_label")
self.mlperrev_tbox = QtWidgets.QTextEdit(Form)
self.mlperrev_tbox.setGeometry(QtCore.QRect(630, 390, 131, 41))
self.mlperrev_tbox.setObjectName("mlperrev_tbox")
self.Dispspeed_label = QtWidgets.QLabel(Form)
self.Dispspeed_label.setGeometry(QtCore.QRect(470, 530, 141, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.Dispspeed_label.setFont(font)
self.Dispspeed_label.setObjectName("Dispspeed_label")
self.Dispspeed_tbox = QtWidgets.QTextEdit(Form)
self.Dispspeed_tbox.setGeometry(QtCore.QRect(630, 520, 131, 41))
self.Dispspeed_tbox.setObjectName("Dispspeed_tbox")
self.start_gcode_tbox = QtWidgets.QTextEdit(Form)
self.start_gcode_tbox.setGeometry(QtCore.QRect(50, 410, 241, 181))
self.start_gcode_tbox.setObjectName("start_gcode_tbox")
self.end_gcode_tbox = QtWidgets.QTextEdit(Form)
self.end_gcode_tbox.setGeometry(QtCore.QRect(50, 640, 241, 191))
self.end_gcode_tbox.setObjectName("end_gcode_tbox")
self.start_gcode_label = QtWidgets.QLabel(Form)
self.start_gcode_label.setGeometry(QtCore.QRect(50, 380, 231, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.start_gcode_label.setFont(font)
self.start_gcode_label.setObjectName("start_gcode_label")
self.end_gcode_label = QtWidgets.QLabel(Form)
self.end_gcode_label.setGeometry(QtCore.QRect(50, 610, 231, 21))
font = QtGui.QFont()
font.setPointSize(10)
self.end_gcode_label.setFont(font)
self.end_gcode_label.setObjectName("end_gcode_label")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.XYcoordinates_label.setText(_translate("Form", "Coordinates;"))
self.X_label.setText(_translate("Form", "X"))
self.Y_label.setText(_translate("Form", "Y"))
self.XYcoordinate_label.setText(_translate("Form", "XY cordinates"))
self.XY_button.setText(_translate("Form", "ADD"))
self.XYspeed_label.setText(_translate("Form", "XY speed"))
self.Gengoce_button.setText(_translate("Form", "Generate Gcode"))
self.Impdepth_label.setText(_translate("Form", "Impression depth"))
self.Displiq_label.setText(_translate("Form", "Dispensing liquid"))
self.Impspeed_label.setText(_translate("Form", "Impression tool speed"))
self.Xoffset_label.setText(_translate("Form", "XY offset"))
self.mlperrev_label.setText(_translate("Form", "ml per revolution"))
self.Dispspeed_label.setText(_translate("Form", "Dispensing speed"))
self.start_gcode_label.setText(_translate("Form", "Start gcode;"))
self.end_gcode_label.setText(_translate("Form", "End gcode;"))
self.XY_button.clicked.connect(self.xycord_gen)
self.Gengoce_button.clicked.connect(self.generate_gcode)
def xycord_gen(self):
x_cord = self.Xcoordinate_tbox.toPlainText()
y_cord = self.Ycoordinate_tbox.toPlainText()
print(f"{x_cord} {y_cord}")
#self.xyspeed()
self.XYcoordinates_tbox.setPlainText(f"X{x_cord} Y{y_cord}\r\n")
'''
def xyspeed(self):
xy_speed = self.XYspeed_tbox.toPlainText()
print(xyspeed)
'''
def generate_gcode(self):
#self.xyspeed()
xy_speed = self.XYspeed_tbox.toPlainText()
imp_depth = self.Impdepth_tbox.toPlainText()
imp_speed = self.Impspeed_tbox.toPlainText()
x_offset = self.Xoffset_tbox.toPlainText()
y_offset = self.Yoffset_tbox.toPlainText()
ml_rev = self.mlperrev_tbox.toPlainText()
disp_liq = self.Displiq_tbox.toPlainText()
disp_speed = self.Dispspeed_tbox.toPlainText()
gcode_start = self.start_gcode_tbox.toPlainText()
gcode_end = self.end_gcode_tbox.toPlainText()
print(f"xy_speed: {xy_speed} mm/sec")
print(f"imp_depth: {imp_depth} mm")
print(f"imp_speed: {imp_speed} mm/sec")
print(f"Offset --> X: {x_offset} Y: {y_offset} mm")
print(f"ml_rev: {ml_rev} ml")
print(f"disp_liquid: {disp_liq} ml")
print(f"disp_speed: {disp_speed} mm/sec")
print(f"start_gcode: {gcode_start}")
print(f"end_gcode: {gcode_end}")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
我可以在文本框中中给出 X 和 Y 位置,然后单击 ADD 显示在坐标上;文本框。我想要的是输入第一个X,Y值,然后单击 ADD ,它应该从文本框中清除当前的X和Y值,以便我可以输入新值。目前,我手动从框中删除旧值以输入新值。另外,输入新值并添加后,它应该显示在坐标; 文本框中,在先前值下方
G28 ; start gcode
_
T0 |
G0 F3000 ; F<xy speed> |
G0 X20 Y20 ; X<x_coord> Y<y_coord> |
G0 F2000 ; F<impression tool speed> |
G92 E0 |
G1 E10 ; E<impression depth> |
G1 E0 |---> Block 1
|
T1 |
G0 X15 Y15 ; X[x_coord - x_offset] Y[y_coord - y_offset] |
G92 E0 |
G1 E10 F500 ; E<10*(dispensing liquid/ml per revolution)> |
G4 P500 |
_
T0 |
G0 F3000 |
G0 X30 Y30 ; X<x_coord> Y<y_coord> here new x and y coordinates|
G0 F2000 |
G92 E0 |
G1 E10 |
G1 E0 |
|->Block2
T1 |
G0 X25 Y25 F1000 ; new x and y coordinates - offset |
G92 E0 |
G1 E10 F500 |
G4 P500 |
;remaining blocks if any
G28 X ;end gcode
以上是我的输出应显示的样子。此代码中的块数将与添加的X,Y坐标对的数目相同。每个块之间的差异仅在于G0 X<x_coord> Y<y_coord>
和G0 X<x_coord - x_offset> Y<y_coord - y_offset>
点击 Generate Gcode (生成Gcode)按钮时,它应该会产生一个与上述代码相似的文件(如果可能,扩展名为.gcode或.txt,在这种情况下,我将其另存为.gcode在记事本中++) )
因此,应该如何保存通过文本框输入的X和Y坐标。元组是一个不错的选择吗?如何将输入的x和y值添加到元组?
我不是这个问题的正确平台。如果没有,请重定向我。
欢迎提出建议。