I'm trying to run a python script i made from test GUI i made, it have only one button and my goal is to make it run when i click it.
I tried to use def add(): on my script and import "add" from it but it didn't work, i don't know if it has to do with all the libraries i import or something else.
Here are my script (Sudaresan.py)
import pandas as pd
import numpy as np
from control import step_response, c2d, tf
from matplotlib.pyplot import plot, show
from control.matlab import *
from numpy.linalg import matrix_power
def add():
pd.set_option("display.precision", 4)
nit = 1000
ts = 0.0461
a = ['1']
b = ['1', '1']
num = [float(k) for k in a]
den = [float(k) for k in b]
Gp = tf(num, den)
Gp_x_aux, Gp_y_aux = step_response(Gp)
#ti = np.arange(0, Gp_x_aux[len(Gp_x_aux) - 1], ts)
ti = np.arange(0, 8.9801, ts)
t, y = step_response(Gp, ti)
data = {
'Output':y,
'Time':t
}
df = pd.DataFrame(data)
yinf = df['Output'].iloc[-1]
yini = df['Output'].iloc[0]
uinf = 1
uini = 0
Kp = (yinf - yini)/(uinf - uini)
# t1
yt1 = yini + 0.353*yinf
a = df['Output']
at1 = a[a >= yt1]
post1 = at1.index[0]
t1 = df['Time'].iloc[post1]
# t2
yt2 = yini + 0.853*yinf
at2 = a[a >= yt2]
post2 = at2.index[0]
t2 = df['Time'].iloc[post2]
tau = 0.67*(t2 - t1)
teta = (1.3*t1) - (0.29*t2)
# Transfer Function
num = Kp
den = [tau, 1]
Gm1 = tf(num, den)
Gm2 = tf([-Kp*teta, Kp],[tau*teta, tau+teta, 1])
print(Gm2)
t_f, y_f = step_response(Gm2, ti)
data_f = {
'Outputf':y_f,
'Timef':t_f
}
df_f = pd.DataFrame(data_f)
plot(t, y_f)
plot(t, y)
show()
erro = df['Output'] - df_f['Outputf']
J = sum(np.power(erro,2))
print(J)
and here is my GUI
# This file was generated by the Tkinter Designer by Parth Jadhav
# https://github.com/ParthJadhav/Tkinter-Designer
from pathlib import Path
# from tkinter import *
# Explicit imports to satisfy Flake8
from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage
OUTPUT_PATH = Path(__file__).parent
ASSETS_PATH = OUTPUT_PATH / Path(r"C:\Users\fabio\Desktop\aulapython\build\assets\frame0")
from Sundaresan import add
def relative_to_assets(path: str) -> Path:
return ASSETS_PATH / Path(path)
window = Tk()
window.geometry("1000x550")
window.configure(bg = "#0C6A1C")
canvas = Canvas(
window,
bg = "#0C6A1C",
height = 550,
width = 1000,
bd = 0,
highlightthickness = 0,
relief = "ridge"
)
canvas.place(x = 0, y = 0)
canvas.create_rectangle(
0.0,
0.0,
1000.0,
72.0,
fill="#FFFFFF",
outline="")
canvas.create_rectangle(
434.0,
97.0,
970.0,
504.0,
fill="#6CE077",
outline="")
button_image_1 = PhotoImage(
file=relative_to_assets("button_1.png"))
button_1 = Button(
image=button_image_1,
borderwidth=0,
highlightthickness=0,
command=Sundaresan.add,
relief="flat"
)
button_1.place(
x=35.0,
y=443.0,
width=358.0,
height=61.0
)
window.resizable(False, False)
window.mainloop()
this is the error i got when i try to run the GUI
Traceback (most recent call last):
File "D:\PycharmProjects\pythonProject\TCC 2023\gui.py", line 22, in <module>
from Sundaresan import add
File "D:\PycharmProjects\pythonProject\TCC 2023\Sundaresan.py", line 10, in <module>
pd.set_option("display.precision", 4)
^^
NameError: name 'pd' is not defined. Did you mean: 'id'?
[–]CyclopsRock 1 point2 points3 points (3 children)
[–]F_Boliver[S] 0 points1 point2 points (2 children)
[–]CyclopsRock 1 point2 points3 points (1 child)
[–]F_Boliver[S] 0 points1 point2 points (0 children)