Question: Problem 2 Slider Look at slider.py. This file has a class called SliderDisplay. By default, the Slider class inside of PyQt only has support for
Problem 2 Slider Look at slider.py. This file has a class called SliderDisplay. By default, the Slider class inside of PyQt only has support for integer values. Our goal here is to implement a slider which can work on arbitrary ranges.
Implement the SliderDisplay class in the file to create a slider which takes in a name (Magnitude in the above) and ranges from low to high with the corresponding number of ticks. It should also take in a string units (defaults to an empty string) which is displayed alongside the numeric reading (dB in the above example). Youll need to take care of the following: Saving down the inputs passed by the user as attributes Creating the display elements, adding them to the layout, and attaching any necessary callbacks to them Computing a correct value based on a linear interpolation and updating the label afterwards Note that the value() method should only return a numeric value. If you want the code to update the label with the corresponding value, you should do that in a different method which calls value(). You should also make sure that the label displays a consistent number of decimal points (3 is fine) so that the size of the window doesnt jump all over the place if the label evaluates to a number like 3.100000000001.
slider.py
#!/usr/bin/env python3 from PyQt5.QtWidgets import QApplication, QWidget, QSlider, QLabel, QVBoxLayout from PyQt5.QtCore import Qt class SliderDisplay(QWidget): def __init__(self, name, low, high, ticks=1000, units=''): QWidget.__init__(self) layout = QVBoxLayout() self.setLayout(layout) # Your code goes in here def value(self): """Return the current value of the slider""" return 0 if __name__ == '__main__': app = QApplication([]) slider = SliderDisplay('foo', 0, 1) slider.show() app.exec_()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
