Code: Alles auswählen
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 29 08:34:08 2016
@author: rk480158
"""
import sys
import matplotlib as mpl
from matplotlib.backends.backend_agg import FigureCanvasAgg
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import pyqtSlot,SIGNAL,SLOT
import matplotlib.pyplot as plt
from matplotlib import rcParams
from sympy.matrices import *
from sympy.printing import *
from IPython.display import display, HTML, Math, Latex, clear_output
# from ipywidgets import widgets
from random import randint
import os
app = QtGui.QApplication(sys.argv)
rcParams['text.usetex'] = True
rcParams['text.latex.preamble'] = r'\usepackage{amsmath}'
x = Matrix()
y = Matrix()
w = QtGui.QWidget()
nm = QtGui.QLineEdit(w)
nm.setGeometry(5,100,40,40)
position = 0
def mathTex_to_QPixmap(mathTex, fs):
#---- set up a mpl figure instance ----
fig = mpl.figure.Figure()
fig.patch.set_facecolor('none')
fig.set_canvas(FigureCanvasAgg(fig))
renderer = fig.canvas.get_renderer()
#---- plot the mathTex expression ----
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.patch.set_facecolor('none')
t = ax.text(0, 0, mathTex, ha='left', va='bottom', fontsize=fs)
#---- fit figure size to text artist ----
fwidth, fheight = fig.get_size_inches()
fig_bbox = fig.get_window_extent(renderer)
text_bbox = t.get_window_extent(renderer)
tight_fwidth = text_bbox.width * fwidth / fig_bbox.width
tight_fheight = text_bbox.height * fheight / fig_bbox.height
fig.set_size_inches(tight_fwidth, tight_fheight)
#---- convert mpl figure to QPixmap ----
buf, size = fig.canvas.print_to_buffer()
qimage = QtGui.QImage.rgbSwapped(QtGui.QImage(buf, size[0], size[1],
QtGui.QImage.Format_ARGB32))
qpixmap = QtGui.QPixmap(qimage)
# return qpixmap.toImage()
# return qimage
return qpixmap
def window():
# app = QtGui.QApplication(sys.argv)
# rcParams['text.usetex'] = True
# rcParams['text.latex.preamble'] = r'\usepackage{amsmath}'
# x = Matrix()
global x,y,nm,w
x = Matrix([randint(-20,20), randint(-20,20), randint(-20,20)])
a1 = latex(x)
# w = QtGui.QWidget()
b = QtGui.QLabel(w)
headerline='$%s$'%a1
# headerline= '$C_{soil}=(1 - n) C_m + \\theta_w C_w$'
bild = mathTex_to_QPixmap(headerline, 15)
label_x = QtGui.QLabel(w)
# pixmap = QPixmap(sys.argv[1])
label_x.setPixmap(bild)
label_x.setGeometry(5,5,50,100)
label_x.show()
y = Matrix()
y = Matrix([randint(-20,20), randint(-20,20), randint(-20,20)])
a2 = latex(y)
headerline='$%s$'%a2
# headerline= '$C_{soil}=(1 - n) C_m + \\theta_w C_w$'
bild = mathTex_to_QPixmap(headerline, 15)
label_y = QtGui.QLabel(w)
# pixmap = QPixmap(sys.argv[1])
label_y.setPixmap(bild)
label_y.setGeometry(50,5,50,100)
label_y.show()
label_aufgabe = QtGui.QLabel(w)
label_aufgabe.setText("Wie lautet das Skalarprodukt?")
label_aufgabe.show()
# nm=QtGui.QLineEdit(w)
# nm.setGeometry(5,100,40,40)
nm.show()
#zahl = nm.text()
b1=QtGui.QPushButton(w)
b1.setGeometry(5,170,70,40)
b1.setText("Berechnen")
# b1.show()
# w.connect(b1,QtCore.SIGNAL("clicked()"),QtGui.qApp,QtCore.SLOT("showdialog()"))
#self.connect(btnQuit, QtCore.SIGNAL('clicked()'),
# QtGui.qApp, QtCore.SLOT('quit()'))
b1.clicked.connect(showdialog)
b1.show()
b2=QtGui.QPushButton(w)
b2.setGeometry(50,100,150,40)
b2.setText("Neuer Durchgang")
b2.clicked.connect(neu)
b2.show()
#nm.textChanged.connect(showdialog)
# if (b1.clicked.connect(self.buttonClicked)):
# showdialog()
w.setGeometry(100,100,400,400)
# b.move(50,20)
w.setWindowTitle("PyQt")
w.show()
sys.exit(app.exec_())
def neu():
# python = sys.executable
# os.execl(python, python, * sys.argv)
os.execv(Skalar_Qt_version3, sys.argv)
def showdialog():
global x,y,nm,w,position
# position = position + 50
label_lsg = QtGui.QLabel(w)
label_lsg.setGeometry(80,80,270,270)
#label_lsg.setText("")
label_lsg.clear()
#print("%s"%nm.text)
z = x.dot(y)
print(z)
parsed = "{0}".format(z)
#print(parsed)
#print(nm.text())
if parsed == nm.text():
ausgabe = "Richtig"
else:
ausgabe = "Falsch"
#
# label_lsg = QtGui.QLabel(w)
# label_lsg.setGeometry(80,80,270,270)
# label_lsg.setText("%s"%ausgabe)
# label_lsg.setGeometry(80,80,170,170)
label_lsg.show()
#print ("Guten Morgen ",x)
if ausgabe == 'Richtig':
msg = QtGui.QMessageBox()
msg.setIcon(QtGui.QMessageBox.Information)
msg.setText("your answer is right")
# msg.setInformativeText("This is additional information")
# msg.setWindowTitle("MessageBox demo")
# msg.setDetailedText("The details are as follows:")
# msg.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel)
msg.exec_()
else:
msg = QtGui.QMessageBox()
msg.setIcon(QtGui.QMessageBox.Information)
msg.setText("your answer is wrong")
msg.exec_()
#msg.buttonClicked.connect(msgbtn)
#retval = msg.exec_()
if __name__ == '__main__':
# w = QtGui.QWidget()
# x = Matrix()
# y = Matrix()
# nm=QtGui.QLineEdit(w)
# nm.setGeometry(5,100,40,40)
window()