Mar-14-2019, 10:38 AM
A first learning script written in Python language by Kevin Brown following a program developed in Power Basic language many years ago. The script is presented here as part of the learning process to invite critique and advise from more experienced Python developers on how to develop and write code following good practice.
Looking forward to comments advise as a new member of this group.
Looking forward to comments advise as a new member of this group.
# A first learning script written in Python language by Kevin Brown following a program developed in Power Basic language # many years ago. The script is presented here as part of the learning process to invite critique and advise from more # experienced Python developers on how to develop code. # # PROGRAM OBJECTIVE : To calculate Hydraulic power, Duty Power and electric Motor nameplate rating of a pump based on the # engineering input values of flow, fluid density, pump differential head ( or inlet and outlet pressure), # estimated pump efficiency % and % margin for electric motor power. # Typical values Flow 600 m3/h Density 1000 kg/m3 Diff Head 300 m of fluid Efficiency 80% Power Margin 10% # # The units of measure (UOM) are currently only SI units m3/h bar m of fluid KW. # Further enhancement should include US Customary units BPD or US Gal/min psig and BHP and saving to random access files or an SQL # data base. # from tkinter import * from datetime import * def donothing(): filewin = Toplevel(window) button = Button(filewin, text=" Put your action code here.") button.pack( padx = 10, pady = 45 ) filewin.geometry('400x200+80+80') def ExitProg() : window.destroy() import tkinter.messagebox as box window = Tk() sv = StringVar() time_stamp = StringVar() ############## window.geometry('570x500+40+40') lbl01Flow = Label(window,relief = 'groove', width = 20, anchor = W) lbl02Dens = Label(window,relief = 'groove', width = 20, anchor = W) lbl03Suct = Label(window,relief = 'groove', width = 20, anchor = W) lbl04Disc = Label(window,relief = 'groove', width = 20, anchor = W) lbl05Diff = Label(window,relief = 'groove', width = 20, anchor = W) lbl06Effi = Label(window,relief = 'groove', width = 20, anchor = W) lbl07Marg = Label(window,relief = 'groove', width = 20, anchor = W) lbl08Hydr = Label(window,relief = 'groove', width = 20, anchor = W) lbl09Duty = Label(window,relief = 'groove', width = 20, anchor = W) lbl09Name = Label(window,relief = 'groove', width = 20, anchor = W) lblHydPwr11 = Label(window,relief = 'groove', width = 10, anchor = W) lblDutPwr12 = Label(window,relief = 'groove', width = 10, anchor = W) lblNamPwr13 = Label(window,relief = 'groove', width = 10, anchor = W) lbl14Desc = Label(window,relief = 'groove', width = 20, anchor = W) lbl15File = Label(window,relief = 'groove', width = 20, anchor = W) lbl16Sugg = Label(window,relief = 'groove', width = 34, anchor = W) EntFlow = Entry(window,relief = 'groove', width = 10) EntDens = Entry(window,relief = 'groove', width = 10) EntSuct = Entry(window,relief = 'groove', width = 10) EntDisc = Entry(window,relief = 'groove', width = 10) EntDiff = Entry(window,relief = 'groove', width = 10) EntEffi = Entry(window,relief = 'groove', width = 10) EntMarg = Entry(window,relief = 'groove', width = 10) sv.trace("w", lambda name, index, mode, sv=sv: callback(sv)) EntDesc = Entry(window, textvariable=sv, width = 32) menubar = Menu(window) filemenu = Menu(menubar, tearoff =0 ) filemenu.add_command(label="New", command=donothing) filemenu.add_command(label="Open", command=donothing) filemenu.add_command(label="Save", command=donothing) filemenu.add_command(label="Save as...", command=donothing) filemenu.add_command(label="Close", command=ExitProg) menubar.add_cascade(label ='File', menu = filemenu) filemenu = Menu(menubar, tearoff =0 ) window.config(menu=menubar) CalcBtn = Button(window) ClrAllBtn = Button(window) ClrOutBtn = Button(window) ClrExitBtn = Button(window) AcceptBtn = Button(window) # Geometry x01 = 20 x02 = 180 x03 = 280 x04 = 440 x05 = 500 x06 = 560 y01 = 60 y02 = 80 y03 = 100 y04 = 120 y05 = 140 y06 = 160 y07 = 180 y08 = 200 y09 = 220 y10 = 240 y11 = 260 y12 = 280 y13 = 300 y14 = 320 y15 = 340 y16 = 360 y17 = 380 y18 = 400 y19 = 420 y20 = 440 lbl01Flow.place(x = x01, y = y01) lbl02Dens.place(x = x01, y = y02) lbl03Suct.place(x = x01, y = y06) lbl04Disc.place(x = x01, y = y07) lbl05Diff.place(x = x01, y = y08) lbl06Effi.place(x = x01, y = y09) lbl07Marg.place(x = x01, y = y10) lbl08Hydr.place(x = x03, y = y08) lbl09Duty.place(x = x03, y = y09) lbl09Name.place(x = x03, y = y10) lblHydPwr11.place(x = x04, y = y08) lblDutPwr12.place(x = x04, y = y09) lblNamPwr13.place(x = x04, y = y10) lbl14Desc.place(x = x01, y = y16) lbl15File.place(x = x01, y = y17) lbl16Sugg.place(x = x02, y = y17) EntFlow.place(x = x02 , y = y01) EntDens.place(x = x02 , y = y02) EntSuct.place(x = x02 , y = y06) EntDisc.place(x = x02 , y = y07) EntDiff.place(x = x02 , y = y08) EntEffi.place(x = x02 , y = y09) EntMarg.place(x = x02 , y = y10) EntDesc.place(x = x02 , y = y16) var = IntVar() var01 = IntVar() # Option to input 2 presssure from which to calcualte differential pressure or # simply to input the differential pressure value. radio1 = Radiobutton(window, text = 'Input Pressures' , variable=var, value=1 ) radio2 = Radiobutton(window, text = 'Input Diff. head', variable=var, value=2 ) # Default is enter differntial pressure value. radio2.select() # Accept the suggested filename with time stamp #check01 = Checkbutton(window, text = 'Accept', variable = var01,onvalue = 1 , offvalue = 0) EntSuct.configure(state = DISABLED) EntDisc.configure(state = DISABLED) CalcBtn.place(x = x02, y = y12) ClrAllBtn.place(x = 280, y = y12) ClrOutBtn.place(x = x04, y = y12) ClrExitBtn.place( x = x04, y = y20) AcceptBtn.place ( x = x04, y = y17) radio1.place (x = x01, y = y03) radio2.place (x = x02, y = y03) #check01.place (x = x04, y = y17) # Static properties window.title('Pump Power Calculator SI units (1)') # Initial window title window.resizable(0,0) CalcBtn.configure( text = 'Calculate Power') ClrAllBtn.configure( text = 'Clear all') ClrOutBtn.configure( text = 'Clear output') ClrExitBtn.configure( text = 'Exit') AcceptBtn.configure( text = 'Accept Filename') # Initial Properties lbl01Flow.configure( text = 'Flow (m3/h)') lbl02Dens.configure( text = 'Density (kg/m3)') lbl03Suct.configure( text = 'Suction (barg)') lbl04Disc.configure( text = 'Discharge (barg)') lbl05Diff.configure( text = 'Diff. Head ( m of fluid)') lbl06Effi.configure( text = 'Est Pump efficiency %') lbl07Marg.configure( text = 'Motor Power Margin %') lbl08Hydr.configure( text = 'Hydraulic Power (KW)') lbl09Duty.configure( text = 'Duty Power (KW)') lbl09Name.configure( text = 'Nameplate Power (KW)') lbl14Desc.configure( text = 'Pump Service Description') lbl15File.configure( text = 'Suggested Filename') # Dynamic Proprties def Calc() : if EntEffi.get() == "": box.showinfo('Input error ','Efficiencey (%) must be greater than 0') if EntMarg.get() == "": box.showinfo('Input error ','Power margin (%) must be greater than 0') if var.get() == 1 : EntDiff.configure(state = NORMAL) EntDiff.delete(0, 'end') Flow = float(EntFlow.get()) Dens = float(EntDens.get()) Suct = float(EntSuct.get()) Disc = float(EntDisc.get()) # p = (0.0981 * SG ) * h # h = p / ( 0.0981 * SG ) Diff = (Disc - Suct) / (0.981 * (Dens)/10000) EntDiff.insert(0, '%.2f' % Diff ) #Diff = float(EntDiff.get()) Effi = float(EntEffi.get()) Marg = float(EntMarg.get()) # Hydraulic Power = flow * Dens * 9.81 * Diff / 3610600 Hydr = ( Flow * Dens * 9.81 * Diff ) / 3610600 lblHydPwr11.configure( text = '%.2f' % Hydr ) #EntDisc.insert(0, '%.2f' % Hydr ) Duty = Hydr * (100/Effi) Name = Duty * ((100 + Marg)/100) lblDutPwr12.configure( text = '%.2f' % Duty ) lblNamPwr13.configure( text = '%.2f' % Name ) else : Flow = float(EntFlow.get()) Dens = float(EntDens.get()) Diff = float(EntDiff.get()) Effi = float(EntEffi.get()) Marg = float(EntMarg.get()) # Hydraulic Power = flow * Dens * 9.81 * Diff / 3610600 Hydr = ( Flow * Dens * 9.81 * Diff ) / 3610600 lblHydPwr11.configure( text = '%.2f' % Hydr ) # EntDisc.insert(0, '%.2f' % Hydr ) Duty = Hydr * (100/Effi) Name = Duty * ((100 + Marg)/100) lblDutPwr12.configure( text = '%.2f' % Duty ) lblNamPwr13.configure( text = '%.2f' % Name ) def ClrAll() : EntFlow.delete(0, 'end') EntDens.delete(0, 'end') EntSuct.delete(0, 'end') EntDisc.delete(0, 'end') EntDiff.delete(0, 'end') EntEffi.delete(0, 'end') EntMarg.delete(0, 'end') EntDens.delete(0, 'end') EntFlow.delete(0, 'end') lblHydPwr11.configure( text = '' ) lblDutPwr12.configure( text = '' ) lblNamPwr13.configure( text = '' ) CalcBtn.configure( state = NORMAL) def inputPress() : ClrInputPress () ClrOutput() EntSuct.configure(state = NORMAL) EntDisc.configure(state = NORMAL) EntDiff.configure(state = DISABLED) def inputDiff() : ClrInputPress () ClrOutput() EntSuct.configure(state = DISABLED) EntDisc.configure(state = DISABLED) EntDiff.configure(state = NORMAL) def ClrOutput() : lblHydPwr11.configure( text = '' ) lblDutPwr12.configure( text = '' ) lblNamPwr13.configure( text = '' ) def ClrInputPress() : EntSuct.delete(0, 'end') EntDisc.delete(0, 'end') EntDiff.delete(0, 'end') def callback(sv): today = datetime.today() time_stamp = (sv.get()+ '_'+ today.strftime('%d') + today.strftime('%B') [0:3]+ today.strftime('%Y') + ' '+ today.strftime('%H')+ '_'+ today.strftime('%M')+ '_'+ today.strftime('%S') ) #print (time_stamp) window.title( time_stamp) lbl16Sugg.configure( text = (time_stamp)) return True def AcceptFile(): EntDesc.configure(state = DISABLED) lbl16Sugg.configure(state = DISABLED) CalcBtn.configure( command = Calc) ClrAllBtn.configure( command = ClrAll) ClrOutBtn.configure( command = ClrOutput) ClrExitBtn.configure ( command = ExitProg) AcceptBtn.configure( command = AcceptFile) radio1.configure ( command = inputPress) radio2.configure ( command = inputDiff) # Sustain window window.mainloop()