-1

I have a text file that assign a numeric value to a parameter with the following line:

designPoint1.SetParameterExpression(Parameter=parameter1, Expression="50")

I want to create a GUI in which user can assign a new value to parameters (for example 20). This GUI has to replace the new value in the previous line so that the result of substitution would be:

designPoint1.SetParameterExpression(Parameter=parameter1, Expression="20")

This is what i've tried to do:

import sys

from tkinter import *

def NewValues():
    diamValue = Diameter.get()
    cpValue = Cp.get()
    cbValue = Cb.get()
    gammaValue = GammaRel.get()
    epsValue = Epsilon.get()
    with open('parametri.txt', 'r') as input_file, open('newparametri.txt', 'w') as output_file:
         for line in input_file:
             if line.strip() == 'designPoint1.SetParameterExpression(Parameter=parameter1, Expression="50")':
                 output_file.write('designPoint1.SetParameterExpression(Parameter=parameter1, Expression="diamValue"\n)')
             else:
                output_file.write(line)
    return
def RunSimulation():
    pass
    return
App = Tk()
Diameter = StringVar()
Cp = StringVar()
Cb = StringVar()
GammaRel = StringVar()
Epsilon = StringVar()
App.geometry("250x200")
App.title("Static Calculator")
AppLabel1 = Label(text="Diameter").grid(row =0,column =0,sticky="W")
AppLabel2 = Label(text="Cp").grid(row=1,column=0,sticky="W")
AppLabel3 = Label(text="Cb").grid(row=2,column=0,sticky="W")
AppLabel4 = Label(text="Gamma Rel").grid(row=3,column=0,sticky="W")
AppLabel5 = Label(text="epsilon").grid(row=4,column=0,sticky="W")
AppEntry1 = Entry(App,textvariable=Diameter).grid(row =0,column =1)
AppEntry2 = Entry(App,textvariable=Cp).grid(row =1,column =1)
AppEntry3 = Entry(App,textvariable=Cb).grid(row =2,column =1)
AppEntry4 = Entry(App,textvariable=GammaRel).grid(row =3,column =1)
AppEntry5 = Entry(App,textvariable=Epsilon).grid(row =4,column =1)
Appbutton1 = Button(App,text = "Update Values",command = NewValues,).grid(row =5,column =1)
Appbutton2 = Button(App,text = "Run",command = RunSimulation,).grid(row =6,column =1)
App.mainloop()

Obviously, the results of this code is:

designPoint1.SetParameterExpression(Parameter=parameter1, Expression="diamValue")

Is it possible to correct this code to reach the goal?

Thanks for every help.

iceeyesvg
  • 23
  • 1
  • 2

2 Answers2

1
output_file.write('designPoint1.SetParameterExpression(Parameter=parameter1, Expression="diamValue"\n)')

Your error is in this line. You need to concatenate/format your string.

Using concatenation,

myVariable = "world"
print("Hello "+myVariable+"!")

Using .format(),

myVariable = "world"
print("Hello {var}!".format(var=myVariable))

About string formatting, you should also check this question.
Pythons many ways of string formatting — are the older ones (going to be) deprecated?

Community
  • 1
  • 1
Lafexlos
  • 7,618
  • 5
  • 38
  • 53
0

As per Lafexlos's example instead of:

output_file.write('designPoint1.SetParameterExpression(Parameter=parameter1, Expression="diamValue"\n)')

You should concatenate the variable and the string like this:

output_file.write('designPoint1.SetParameterExpression(Parameter=parameter1, Expression="'+ diamValue +'"\n)')

or using the .format() method

output_file.write('designPoint1.SetParameterExpression(Parameter=parameter1, Expression="{0}"\n)'.format(diamValue))

You may need to format the variable as a string using the first example though: str(variablename)

Leroy
  • 430
  • 8
  • 12
  • Really i don't know how to thank you. It works! That's fantastic. The solution is: output_file.write('designPoint1.SetParameterExpression(Parameter=parameter1, Expression="'+ str(diamValue) +'")\n') – iceeyesvg Oct 14 '14 at 11:43