Python Forum
TKinter Slider widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TKinter Slider widget
#1
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.


# 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()
Reply
#2
see this: https://www.python-course.eu/tkinter_sliders.php
You can use text 'ticks' (hyphens for vertical scale, "'" for horizontal scale) if you don't like numbers
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TKinter Widget Attribute and Method Quick Reference zunebuggy 3 849 Oct-15-2023, 05:49 PM
Last Post: zunebuggy
  neeed to activate a custom tk slider janeik 1 832 Oct-09-2023, 09:29 AM
Last Post: deanhystad
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,858 Jun-26-2022, 06:26 PM
Last Post: menator01
  Tkinter Exit Code based on Entry Widget Nu2Python 6 2,989 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  tkinter text widget word wrap position chrisdb 6 7,567 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  tkinter slider question Nick_tkinter 1 2,489 Feb-22-2021, 01:31 PM
Last Post: deanhystad
  Tkinter - How can I extend a label widget? TurboC 2 2,778 Oct-13-2020, 12:15 PM
Last Post: zazas321
  [Tkinter] Tkinter custom widget styling and creating custom theme karolp 6 4,833 May-06-2020, 06:11 PM
Last Post: karolp
  Tkinter calendar widget scratchmyhead 4 4,335 May-03-2020, 07:01 PM
Last Post: scratchmyhead
  [PyGUI] Python 3.8.1 Tkinter Widget stete change sasiap 4 2,522 Feb-14-2020, 10:38 AM
Last Post: sasiap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020