here2share

# Tk_wave_controls.py

Jun 24th, 2020
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. # Tk_wave_controls.py
  2.  
  3. from Tkinter import *
  4. import math
  5.  
  6. root = Tk()
  7. root.title("Tk Wave Controls")
  8.  
  9. ww=800
  10. hh=600
  11. fram = Frame(root)
  12. Label(fram,text='f(x):').pack(side=LEFT)
  13. func = Entry(fram, width=50)
  14. func.pack(side=LEFT, fill=BOTH, expand=1)
  15. fram.pack(side=TOP)
  16.  
  17. fram = Frame(root)
  18.  
  19. bbxy = 'minX', 'maxX', 'minY', 'maxY'
  20.  
  21. bounds = []
  22. for label in bbxy:
  23.     Label(fram,text=label+':').pack(side=LEFT)
  24.     edit = Entry(fram, width=6)
  25.     edit.pack(side=LEFT)
  26.     bounds.append(edit)
  27. fram.pack(side=TOP)
  28.  
  29. c = Canvas(root, width=ww, height=hh, bg='green')
  30. c.pack(side=TOP, fill=BOTH, expand=1)
  31.  
  32. def minimax(values=[0.0, 10.0, -2.0, 2.0]):
  33.     for i in range(4):
  34.         edit = bounds[i]
  35.         try: values[i] = float(edit.get())
  36.         except: pass
  37.         edit.delete(0, END)
  38.         edit.insert(END, '%.2f'%(values[i]*0.1))
  39.     return values
  40.  
  41. def plot(nil=0):
  42.     minx, maxx, miny, maxy = [z*0.1 for z in minimax(values)]
  43.  
  44.     f = func.get()
  45.     f = compile(f, f, 'eval')
  46.  
  47.     CX = c.winfo_width()
  48.     if CX/5 > 4:
  49.         CY = c.winfo_height()
  50.         c.delete(ALL)
  51.         for i in range(0,CX):
  52.             x = minx + ((maxx-minx)*i)/CX
  53.             y = eval(f, vars(math), {'x':x})
  54.             j = CY*(y-miny)/max(0.01,(maxy-miny))
  55.             c.create_line(i,j,i,CY,fill='purple')
  56.  
  57.  
  58. f = 'sin(x) + cos(x) + (x**2*0.01)'
  59. func.insert(END, f)
  60. values = minimax()
  61.  
  62. SlideTest = 1
  63.  
  64. if SlideTest:
  65.     root2 = Tk()
  66.     root2.title("Tk Qwikfix Sliders")
  67.     vars_init = bbxy
  68.  
  69.     def refresh():
  70.         try:
  71.             canvas.update()
  72.         except:
  73.             pass
  74.  
  75.     def getSliderValues(nil=0):
  76.         global SlideTest
  77.         if SlideTest > len(bbxy):
  78.             sliderVector = []
  79.             for z in range(len(cvars)):
  80.                 t = w[z].get()
  81.                 edit = bounds[z]
  82.                 edit.delete(0, END)
  83.                 edit.insert(END, '%.2f'%t)
  84.                 sliderVector.append('%.2f'%(float(t)/10))
  85.             plot()
  86.             print '\t\t'.join(sliderVector)
  87.         else:
  88.             SlideTest += 1
  89.  
  90.     sliderframes = Frame(root2)
  91.     sliderframes.pack(side=TOP)
  92.  
  93.     slider = {}
  94.     cvars = {}
  95.  
  96.     iframe = Frame(root2)
  97.     iframe.pack(side=BOTTOM)
  98.  
  99.     w = {}
  100.     for z in range(len(vars_init)):
  101.         VAR = vars_init[z].replace(' ','')
  102.         slider[z] = Frame(sliderframes)
  103.         slider[z].pack(side=TOP)
  104.         cvars[z] = DoubleVar()
  105.         FROM, TO, L = -500, 500, 1000
  106.         if ',' in VAR:
  107.             t = VAR.split(',')[0]
  108.             VAR = "'"+t+"'"+VAR[len(t):]
  109.             VAR, FROM, TO = eval(VAR)
  110.         w[z] = Scale(slider[z], from_=FROM, to=TO, variable=cvars[z], length=L, orient=HORIZONTAL, command=getSliderValues)
  111.         w[z].set(values[z]*10)
  112.         w[z].pack(side=TOP)
  113.         L = Label(slider[z], text=VAR)
  114.         L.pack(side=TOP)
  115.  
  116.     getSliderValues()
  117.  
  118.  
  119. root.mainloop()
Add Comment
Please, Sign In to add comment