Jan-29-2019, 01:28 PM
(This post was last modified: Jan-29-2019, 01:28 PM by Jemeronimo.)
Hey guys! For school I'm making a program in Tkinter with some Sliders. My code is far from nice but I'm doing this all by myself so don't be too hard on me please ^^
I want to add ticks to my slider. Basically I want it to say 0 all on the left and x_o all on the right. Can someone help me doing this? I've been looking around on the internet for a while but can't find it.
Any other tips are very welcome as well.
I want to add ticks to my slider. Basically I want it to say 0 all on the left and x_o all on the right. Can someone help me doing this? I've been looking around on the internet for a while but can't find it.
Any other tips are very welcome as well.
# Import everything import numpy as np import math as math import matplotlib import tkinter as Tk import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg # Set up for the App pi = math.pi matplotlib.use('TkAgg') root = Tk.Tk() root.wm_title("Name of file") fig = plt.Figure() canvas = FigureCanvasTkAgg(fig, root) canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) # Make subplots ax1 = fig.add_subplot(221) ax2 = fig.add_subplot(222) ax3 = fig.add_subplot(223) ax4 = fig.add_subplot(224) fig.subplots_adjust(bottom=0.2, hspace=0.75) x = np.linspace(0, 100, 1000) x2 = np.linspace(0, 3*pi, 1000) font = {'family': 'serif', 'color': 'darkred', 'weight': 'normal', 'size': 10, } def func1(t, omega, position): return (position-0.5) * np.cos(math.pi*t/40+omega+pi/2)+0.5 def func(sr, k, l): return 2*sr*np.cos(k*l*10) # Set up axes ax1.plot(x, func1(x, 0, 0.75), 'k', label="func1") ax1.plot(x, func1(x, pi, 0.75), 'r', label="func2") ax1.legend(loc="upper left", bbox_to_anchor=(0.8, 1)) ax1.axis([6*pi, 20*pi, 0, 1]) ax1.set_title("Behavior of a standard D.C.", fontdict=font, fontsize='12') ax1.set_xlabel("k", fontdict=font) ax1.set_ylabel("Splitting ratio", fontdict=font) ax2.plot(x, func(1, x, 0.1)*func1(x, 0, 0.75), 'k') ax2.plot(x, func(1, x, 0.11)*func1(x, pi, 0.75), 'r') ax2.axis([6*pi, 20*pi, -1.5, 1.5]) ax2.set_title("Cosine wave functions", fontdict=font, fontsize='12') ax2.set_xlabel("$k$", fontdict=font) ax2.set_ylabel("$\psi$(k)", fontdict=font) ax3.plot(x, func(0.5, x, 0.1), 'k') ax3.set_title("Intensity", fontdict=font, fontsize='12') ax3.set_xlabel("Wavenumber k", fontdict=font) ax3.set_ylabel("Intensity", fontdict=font) ax3.axis([6*pi, 20*pi, -1.5, 1.5]) ax4.plot(x, np.fft.fft(func(0.5, x, 0.1)), "k") ax4.axis([-5, max(x)+5, -5, 500]) ax4.set_title("Fourier transform", fontdict=font, fontsize='12') ax4.set_xlabel("Depth (z)", fontdict=font) ax4.set_ylabel("Reflectivity", fontdict=font) # Make Sliders s1_value = fig.add_axes([0.125, 0.52, 0.35, 0.02]) s_1 = Slider(s1_value, '\nSplitting Ratio\n (SR)', 0.25, 0.75, valfmt='%0.2f', valinit=0.75) s2_value = fig.add_axes([0.125, 0.1, 0.35, 0.02]) s_2 = Slider(s2_value, '$\Delta$ L', 0.1, 2*pi, valinit=0.1) def update(val): # Clear all and set again ax1.clear() ax2.clear() ax3.clear() ax4.clear() pos_s1 = s_1.val pos_s2 = s_2.val ax1.plot(x, func1(x, 0, pos_s1), label="func1") ax1.plot(x, func1(x, pi, pos_s1), label="func2") ax1.legend(loc="upper left", bbox_to_anchor=(0.8, 1)) ax1.set_title("Behavior of a standard D.C.") ax1.set_xlabel("k") ax1.set_ylabel("Splitting ratio") ax1.axis([6 * pi, 20 * pi, 0, 1]) ax2.plot(x, func(1, x, 0.1) * func1(x, 0, pos_s1)) ax2.plot(x, func(1, x, 0.1) * func1(x, pi, pos_s1)) ax2.set_title("Cosine wave functions") ax2.set_xlabel("k") ax2.set_ylabel("$\psi$(k)") ax2.axis([6 * pi, 20 * pi, -1.5, 1.5]) ax3.plot(x, func(0.5, x, pos_s2)) ax3.set_title("Intensity") ax3.set_xlabel("Wavenumber k") ax3.set_ylabel("Intensity") ax3.axis([6 * pi, 20 * pi, -1.5, 1.5]) ax4.plot(x, np.fft.fft(func(1, x, pos_s2) * func1(x, 0, pos_s1))) ax4.set_title("Fourier transform ") ax4.set_xlabel("Depth (z)") ax4.set_ylabel("Reflectivity") ax4.axis([-5, max(x)+5, -2, 500]) resetax = fig.add_axes([0.8, 0.025, 0.1, 0.04]) button = Button(resetax, 'Reset', hovercolor='0.975') def reset(event): s_1.reset() s_2.reset() button.on_clicked(reset) s_1.on_changed(update) s_2.on_changed(update) Tk.mainloop()