Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_wave_controls.py
- from Tkinter import *
- import math
- root = Tk()
- root.title("Tk Wave Controls")
- ww=800
- hh=600
- fram = Frame(root)
- Label(fram,text='f(x):').pack(side=LEFT)
- func = Entry(fram, width=50)
- func.pack(side=LEFT, fill=BOTH, expand=1)
- fram.pack(side=TOP)
- fram = Frame(root)
- bbxy = 'minX', 'maxX', 'minY', 'maxY'
- bounds = []
- for label in bbxy:
- Label(fram,text=label+':').pack(side=LEFT)
- edit = Entry(fram, width=6)
- edit.pack(side=LEFT)
- bounds.append(edit)
- fram.pack(side=TOP)
- c = Canvas(root, width=ww, height=hh, bg='green')
- c.pack(side=TOP, fill=BOTH, expand=1)
- def minimax(values=[0.0, 10.0, -2.0, 2.0]):
- for i in range(4):
- edit = bounds[i]
- try: values[i] = float(edit.get())
- except: pass
- edit.delete(0, END)
- edit.insert(END, '%.2f'%(values[i]*0.1))
- return values
- def plot(nil=0):
- minx, maxx, miny, maxy = [z*0.1 for z in minimax(values)]
- f = func.get()
- f = compile(f, f, 'eval')
- CX = c.winfo_width()
- if CX/5 > 4:
- CY = c.winfo_height()
- c.delete(ALL)
- for i in range(0,CX):
- x = minx + ((maxx-minx)*i)/CX
- y = eval(f, vars(math), {'x':x})
- j = CY*(y-miny)/max(0.01,(maxy-miny))
- c.create_line(i,j,i,CY,fill='purple')
- f = 'sin(x) + cos(x) + (x**2*0.01)'
- func.insert(END, f)
- values = minimax()
- SlideTest = 1
- if SlideTest:
- root2 = Tk()
- root2.title("Tk Qwikfix Sliders")
- vars_init = bbxy
- def refresh():
- try:
- canvas.update()
- except:
- pass
- def getSliderValues(nil=0):
- global SlideTest
- if SlideTest > len(bbxy):
- sliderVector = []
- for z in range(len(cvars)):
- t = w[z].get()
- edit = bounds[z]
- edit.delete(0, END)
- edit.insert(END, '%.2f'%t)
- sliderVector.append('%.2f'%(float(t)/10))
- plot()
- print '\t\t'.join(sliderVector)
- else:
- SlideTest += 1
- sliderframes = Frame(root2)
- sliderframes.pack(side=TOP)
- slider = {}
- cvars = {}
- iframe = Frame(root2)
- iframe.pack(side=BOTTOM)
- w = {}
- for z in range(len(vars_init)):
- VAR = vars_init[z].replace(' ','')
- slider[z] = Frame(sliderframes)
- slider[z].pack(side=TOP)
- cvars[z] = DoubleVar()
- FROM, TO, L = -500, 500, 1000
- if ',' in VAR:
- t = VAR.split(',')[0]
- VAR = "'"+t+"'"+VAR[len(t):]
- VAR, FROM, TO = eval(VAR)
- w[z] = Scale(slider[z], from_=FROM, to=TO, variable=cvars[z], length=L, orient=HORIZONTAL, command=getSliderValues)
- w[z].set(values[z]*10)
- w[z].pack(side=TOP)
- L = Label(slider[z], text=VAR)
- L.pack(side=TOP)
- getSliderValues()
- root.mainloop()
Add Comment
Please, Sign In to add comment