有一个程序преобразования температуры из градусов цельсия в фаренгейт и обратно。在第一个输入字段中,用户必须输入一个值,单击所需的按钮进行转换,转换结果应出现在下面的表格中(带有题字答案):
此外,在结果以第二种形式出现后,应以最低绘制形式绘制一个矩形(比例),其长度将等于温度变换的结果:
它应该是这样的:
但我做不到。代码会在下面,现在我给大家讲一下这段代码绘制的大致原理:类中
的方法一直被激活(这段代码我是从别的网站复制过来的,原理我也不是很懂这样的工作,但一般意思是这样),当程序第一次启动时()方法启动,它绘制“温度计主体”并将变量更改为。paintEventFormBuilderself.launch = "first"draw_thermometerself.launch"second"
现在,当方法被激活时paintEvent,方法被执行fill_thermometer,但是温度计的“刻度”(如上图,红色刻度)的绘制不会发生。
请帮我解决这个问题。如果有任何帮助,我将非常高兴!
PS 代码的重要部分(负责绘制)用注释标记# важный метод!
导入系统
从 PyQt5.QtWidgets 导入 QWidget、QPushButton、QLabel、QHBoxLayout、QVBoxLayout、QLineEdit、QFormLayout、\
QGroupBox、QMainWindow、QGridLayout、QApplication
从 PyQt5.QtCore 导入 Qt
从 PyQt5.QtGui 导入 QFont、QPainter、QColor、QPen、QIcon
窗口高度 = 210
窗口宽度 = 300
WINDOW_NAME = '温度'
WINDOW_ICON_NAME = '图片\\main_logo.png'
MAIN_FONT = '时间'
FONT_HEADER_SIZE = 13
FONT_BODY_SIZE = 8
类FormBuilder(QWidget):
def __init__(self, parent=None):
super().__init__(父)
self.launch = "第一"
self.create_all_forms()
self.fill_forms()
self.create_layers()
def create_all_forms(self):
self.heading_label = QLabel()
self.entering_label = QLabel()
self.first_value = QLineEdit(placeholderText="Value T")
self.second_value = QLineEdit(placeholderText="Answer")
self.celc_button = QPushButton("转摄氏度")
self.far_button = QPushButton("到华氏度")
self.first_draw_label = QLabel(" ")
self.second_draw_label = QLabel(" ")
定义填充表格(自我):
self.heading_label.setText("CF 温度换算")
self.heading_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE, QFont.Bold))
self.entering_label.setText("温度值")
self.entering_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE))
self.second_value.setDisabled(True)
self.celc_button.setMinimumWidth(125)
self.celc_button.clicked.connect(self.make_celc)
self.far_button.clicked.connect(self.make_far)
self.first_draw_label.resize(500, 500)
def create_layers(self):
main_layout = QVBoxLayout(self)
标题标签 = QHBoxLayout()
heading_label.addWidget(self.heading_label)
main_layout.addLayout(heading_label)
self.first_groupbox = QGroupBox(self.entering_label.text())
self.first_groupbox.setStyleSheet('''
QGroupBox {
保证金顶部:2ex;
}
QGroupBox:启用{
边框:3px 纯灰色;
边框半径:5px
}
QGroupBox::title {
subcontrol-origin:边距;
左:3ex;
}
''')
first_groupbox_layout = QVBoxLayout(self.first_groupbox)
first_groupbox_layout.addWidget(self.first_value)
first_groupbox_layout.addWidget(self.second_value)
main_layout.addWidget(self.first_groupbox)
button_line = QFormLayout()
button_line.addRow(self.celc_button, self.far_button)
main_layout.addLayout(button_line)
first_draw_label = QHBoxLayout()
first_draw_label.addWidget(self.first_draw_label)
main_layout.addLayout(first_draw_label)
second_draw_label = QHBoxLayout()
second_draw_label.addWidget(self.second_draw_label)
main_layout.addLayout(second_draw_label)
def paintEvent(self, e): # 重要方法!
qp = QPainter()
qp.开始(自我)
如果 self.launch == “第一”:
self.draw_thermometer(qp)
elif self.launch == “第二”:
self.fill_thermometer(qp)
qp.end()
def draw_thermometer(self, qp): # 重要方法!
col = QColor(0, 0, 0)
col.setNamedColor('#d4d4d4')
qp.setPen(col)
qp.setBrush(QColor(112, 106, 104))
qp.drawRoundedRect(10, 150, 260, 35, 5, 5)
qp.setBrush(QColor(194, 189, 186))
qp.drawRect(15, 155, 250, 25)
self.launch = 2
def make_celc(self):
尝试:
如果浮动(self.first_value.text())212:
self.first_value.setText("212")
self.second_value.setText("答案")
结果 = round((float(self.first_value.text()) - 32) * (5 / 9), 3)
self.second_value.setText(str(result))
self.launch = "第二"
除了:
self.second_value.setText("无效输入!")
def make_far(self):
尝试:
如果浮动(self.first_value.text())100:
self.first_value.setText("100")
self.second_value.setText("答案")
结果 = round((float(self.first_value.text()) * (9 / 5)) + 32, 3)
self.second_value.setText(str(result))
self.launch = "第二"
除了:
self.second_value.setText("无效输入!")
def fill_thermometer(self, qp): # 重要方法!
col = QColor(0, 0, 0)
col.setNamedColor('#d4d4d4')
qp.setPen(col)
qp.setBrush(QColor(109, 160, 201))
rect_lenght = round(float(self.second_value.text()))
qp.drawRect(17, 157, rect_lenght, 21)
类主窗口(QMainWindow):
def __init__(self):
超级().__init__()
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.build_skeleton_with_basic_settings()
self.create_and_connect_grid()
def build_skeleton_with_basic_settings(self):
self.setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT)
self.setWindowTitle(WINDOW_NAME)
self.setWindowIcon(QIcon(WINDOW_ICON_NAME))
def create_and_connect_grid(self):
self.main_grid = QGridLayout(self.centralWidget)
form_builder_object = FormBuilder()
self.main_grid.addWidget(form_builder_object, 0, 0, Qt.AlignTop)
如果 __name__ == '__main__':
应用程序 = QApplication(sys.argv)
例如 = 主窗口()
ex.show()
sys.exit(app.exec_())



我试着为你做点什么,如果你不睡觉-试试。