Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Slider GUI help
#1
I have a HW assignment where I need to control volume in a tank. I need a slider scale to set the desired height of the tank. If the desired tank height is higher than the actual then it will increase and vice versa. To accomplish this I need to be able to get the value of the slider as it is changed. For this part of the assignment I wrote this code however when run and the slider position is changed the GUI crashes. Any tips on how to correct this would be appreciated. Here is the code for the slider and a label to hold the slider value.
from tkinter import *
####################
root = Tk()
root.title('Tank Slider')
root.geometry('400x400')
######################
def StartC():
    while ( 2 > 1 ):
        SliderVal()

def SliderVal():
    Svalue["text"]=Slider.get()
############################
Slider = Scale(root, from_=100, to=0, length=200,bg='#80c1ff')
Slider.pack()

Svalue = Label(root,text=" ")
Svalue.pack()

StartB = Button(root,text="Start", command=StartC)
StartB.pack()
###########################
root.mainloop()
Reply
#2
For me (in Mint 20, Python 3.8.5) it works.
Reply
#3
(Dec-02-2020, 04:18 PM)Axel_Erfurt Wrote: For me (in Mint 20, Python 3.8.5) it works.

Hello Axel, sorry I posted the wrong code. I want it to automatically update the label as I change the slider after 1 initial start press. I have updated the code to the one which makes my GUI crash. I know it has to do with the while statement I made but im not sure of a better way to make it update automatically.
Reply
#4
while ( 2 > 1 )

what's that?

2 is always bigger than 1

What do you want to do?
Reply
#5
You should only have one "forever loop" in a tkinter program and that is the mainloop(). The while loop in StartC() locks up you GUI because it is preventing mainloop() from running. If you want to update the tank level to follow the slider you need to periodically call a function that gets the slider value, calculates a new tank level, and updates the level display (Svalue). This function should run for a very short time so it does not interfere with mainloop().

You could specify a command for the slider and always update Svalue whenever the slider is changed. This will update Svalue to be the same as Slider.get(), but it would be more realistic if you had the tank "fill" to the requested level. To fill the tank you need to periodically call a function that "drives" the tank level (float(Svalue['text']) toward the tank level command (Slider['value']).

An easy way to periodically call a function in tkinter is use "after". Here's a small change to your code that will update the Svalue every second after you press the Start button.
def StartC():
    Svalue['text'] = str(Slider.get())
    root.after(1000, StartC)
How could you change this function to ramp Svalue toward Slider.get() instead of taking one big step?
Reply


Forum Jump:

User Panel Messages

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