Мне надо что бы из класса Input
, при правильном вводе и нажатии кнопки, вызвалась функция output_res
которая находится в классе Output
, я новичок так что будьте готовы к говнокоду)
файл gui
:
from tkinter import *
from calculation import *
class Output(Frame):
def __init__(self, root, output_res=None):
super().__init__(root)
self.grid(row=1, column=0)
self.res = output_res
self.out()
def out(self):
self.OFFSET_X = 60
self.OFFSET_Y = 50
self.canv = Canvas(self, bg="white", width=700, height=700)
self.canv.pack()
def output_res(self, res, prob):
DIMEN_COORDPL = 500 # dimension of the coordinate plane
OFFSET_X = self.OFFSET_X
OFFSET_Y = self.OFFSET_Y
STEP_X = DIMEN_COORDPL / 32 # 32 is number of cards
self.canv.create_text(OFFSET_X, DIMEN_COORDPL + OFFSET_Y + 50, text = res)
self.canv.create_line(OFFSET_X, OFFSET_Y,
OFFSET_X,OFFSET_Y + DIMEN_COORDPL, width=2)
self.canv.create_line(OFFSET_X, OFFSET_Y + DIMEN_COORDPL,
OFFSET_X + DIMEN_COORDPL + STEP_X, OFFSET_Y + DIMEN_COORDPL, width=2)
MAXVALUE_Y = max(prob.values())
#y / Y * DIMEN_COORDPL
#x * STEP_X
iter = tuple(prob)
for i in iter:
term_value = prob[i] / MAXVALUE_Y * DIMEN_COORDPL # hight
prob.update({i:term_value})
iter = iter[:len(iter) - 1]
self.canv.create_text(OFFSET_X - 20, OFFSET_Y, text="%0.3f" % (MAXVALUE_Y))
self.canv.create_text(OFFSET_X - 20, OFFSET_Y + DIMEN_COORDPL / 2, text="%0.3f" % (MAXVALUE_Y / 2))
for i in iter:
self.canv.create_text(OFFSET_X + (i-1) * STEP_X,
DIMEN_COORDPL + OFFSET_Y + 15,
text = str(i))
self.canv.create_line(OFFSET_X + (i-1) * STEP_X,
DIMEN_COORDPL + OFFSET_Y - prob[i],
OFFSET_X + (i) * STEP_X,
DIMEN_COORDPL + OFFSET_Y - prob[i + 1],
width=2.5, fill="red")
else:
self.canv.create_text(OFFSET_X + (33) * STEP_X,
DIMEN_COORDPL + OFFSET_Y + 15,
text = str(34))
class Input(Frame):
def __init__(self, root):
super().__init__(root)
self.output_res = None
self.grid(row=0, column=0)
self.input_num_exp()
def input_num_exp(self):
self.entry = Entry(self, bg="red")
self.bat = Button(self, text = "ok", width=4, relief = FLAT)
self.bat.bind('<Button-1>', self.click)
self.bat.grid(row=0, column=0)
self.entry.grid(row=0, column=1)
def click(self, _):
try:
num = int(self.entry.get())
if num <= 0:
raise ValueError
res, prob = calculation(num)
# вызов функции output_res с аргументами res, prob
except ValueError:
pass
def main():
root = Tk()
root.geometry('700x850+10+10')
root.minsize(700, 700)
root.maxsize(850, 850)
inp = Input(root)
out = Output(root)
root.mainloop()
if __name__ == "__main__":
main()
файл calculation
(можете на него не обращать внимания он просто возвращает строку и число):
from gui import *
from math import sqrt
from numpy import zeros, ones, hstack, random, nonzero
def probability(x, num_exp):
for i in x:
x[i] /= num_exp
return x
def calculation(num_exp):
expected_math = 0
expected_mathematical_pow2 = 0
x = {i:0 for i in range(1, 35)}
a = ones(4, int)
b = zeros(32, int)
place_tuze = hstack([a,b])
for i in range(num_exp):
random.shuffle(place_tuze)
term = nonzero(place_tuze == 1)[0][0] + 1
expected_math += term
expected_mathematical_pow2 += term ** 2
x[term] += 1
expected_math /= num_exp
expected_mathematical_pow2 /= num_exp
prob = probability(x, num_exp)
res = ("M[x] = \t" + "%0.6f" % (expected_math) + "\n")
disp = expected_mathematical_pow2 - expected_math ** 2
res += ("D[x] = \t" + "%0.6f" % (disp) + "\n")
res += ("σ = \t" + "%0.6f" % (sqrt(disp)))
return res, prob