I have a few questions about python In the code I will add below, I want to draw with qpainter in qlabel and I want it to happen when the button is clicked.But I couldn't assign it to the button anyway. When the form is opened, the code works directly. How to change it and run it only when the button is clicked.Also when I click this button, I will make a calculation and then make this drawing.
from PyQt5 import QtGui
from math import *
from Dere_Kesit_Hesabi_python import Ui_MainWindow
import os
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class dere_example(QMainWindow):
def __init__(self):
super().__init__()
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.setFixedSize(self.size())
n=0
h=0
b=0
Q100=0
Q500=0
J=0
m=0
user = os.environ['USERNAME']
self.setWindowTitle("Dere Kesit Hesabı-" + user)
self.ui.btnhesap.clicked.connect(self.hesapkitap)
self.ui.btnhesap.clicked.connect(self.paintEvent)
def paintEvent(self, e):
pixmap = QPixmap(self.ui.lblciz.size())
pixmap.fill(Qt.transparent)
painter = QPainter(pixmap)
painter.setPen(QPen(Qt.blue, 2, Qt.SolidLine))
x = 100
y = 100
h=3
b=4
painter.drawLine(x, y, x + h, y - h)
painter.drawLine(x + h, y - h, x + b + h, y - h)
painter.drawLine(x + b + h, y - h, x + b + 2 * h, y)
self.ui.lblciz.setPixmap(pixmap)
painter.end()
def hesapkitap(self):
global n
global h
global b
global Q100
global Q500
global J
global m
try:
n = float(self.ui.txtn.text().replace(",", "."))
Q500 = float(self.ui.txtq500.text().replace(",", "."))
Q100 = float(self.ui.txtq100.text().replace(",", "."))
j = float(self.ui.txtj.text().replace(",", "."))
h = float(self.ui.txth.text().replace(",", "."))
b = float(self.ui.txtb.text().replace(",", "."))
byan = sqrt(h * h + h * h)
büst = 2 * h + b
A = round(((büst + b) / 2 * h), 4)
Ç = round((b + 2 * byan), 4)
R = round((A / Ç), 4)
V = round((1 / n * pow(R, (2 / 3)) * pow(j, (1 / 2))), 4)
Q = round((A * V), 2)
self.ui.lblAlan.setText(str(A) + " m2")
self.ui.lblcevre.setText(str(Ç) + " m")
self.ui.lblyaricap.setText(str(R))
self.ui.lblhiz.setText(str(V) + " s")
self.ui.lblmaxdebi.setText(str(Q) + " m3/s")
h500 =round(( Q500 * h / Q),2)
h1000 = round((Q100 * h / Q),2)
if (Q500 <= Q):
self.ui.lblQ500sonuc.setText("Q500 : "+str(Q500)+" < "+"Q : "+str(Q)+" olduğundan kesit gelen Q500 debisi için yeterli!")
self.ui.lblQ500sonuc.setStyleSheet("color:rgb(0,150,0)")
else:
self.ui.lblQ500sonuc.setText("Q500 : "+str(Q500)+" > "+"Q : "+str(Q)+" olduğundan kesit gelen Q500 debisi için yetersiz!")
self.ui.lblQ500sonuc.setStyleSheet("color:rgb(255,0,0)")
if (Q100 <= Q):
self.ui.lblQ100sonuc.setText("Q100 : "+str(Q100)+" < "+"Q : "+str(Q)+" olduğundan kesit gelen Q100 debisi için yeterli!")
self.ui.lblQ100sonuc.setStyleSheet("color:rgb(0,150,0)")
else:
self.ui.lblQ100sonuc.setText("Q100 : "+str(Q100)+" > "+"Q : "+str(Q)+" olduğundan kesit gelen Q100 debisi için yetersiz!")
self.ui.lblQ100sonuc.setStyleSheet("color:rgb(255,0,0)")
except:
self.ui.statusbar.showMessage("Lütfen bilgileri eksiksiz doldurunuz!", 3000)
self.ui.statusbar.setStyleSheet("color:rgb(255,0,0)")