Tkinter Scale Basic
Python’s tkinter module has the Widget control Scale, which is a graphical interface input function that allows us to move the scale bar to produce a range of numbers.
The method for creating a scale bar is Scale( ), and the construction method for Scale is as follows.
Scale(master, options, ...)
The first argument to the Scale( ) method is the parent object, indicating which parent object this scale bar will be built inside. The following are the other commonly used options parameters within the Scale( ) method.
(1) activebackground
: the background color of the mouse cursor when it is on the scale bar.
(2) bg
: background color.
(3) borderwidth or bd
: the default 3D border width is two pixels.
(4) command
: When the user changes the value, this function will be executed automatically.
(5) cursor
: when the mouse cursor on the scale bar when the cursor shape.
(6) digits
: scale value, read when you need to use IntVar, DoubleVar or StringVar variable type read.
(7) fg
: the foreground color of the text.
(8) font
: font shape.
(9) from_
: initial value of the scale bar range value.
(10) highlightbackground
: the background color when the scale bar gets the focus.
(11) highlightcolor
: the color of the scale bar when it gets the focus.
(12) label
: default is no label text, if the scale bar is horizontal, the label will appear in the upper left corner, if the scale bar is vertical, the label will appear in the upper right corner.
(13) length
: default is 100 pixels.
(14) orient
: default is horizontal, you can set horizontal HORIZONTAL or vertical VERTICAL.
(15) relief
: default is FLAT, you can change the appearance of the border from this.
(16) repeatdelay
: you can set how long you need to hold the scale bar before you can move this scale bar, the unit is ms, the default is 300.
(17) MARKDOWN_HASHb7e164b34ff76b1cda93a058604190daMARKDOWNHASH
: the value of each change, for example, from\=2.0, to=4.0, if the resolution is set to 0.5, then the scale may be 2.0, 2.5, 3.0, 3.5, 4.0.
(18) showvalue
: normal will display the current value of the scale bar, if set to 0, it will not be displayed.
(19) state
: If set to DISABLE, this scale is temporarily unavailable.
(20) takefocus
: the scale bar can cycle to get the focus normally, if set to 0, it can not get the focus.
(21) MARKDOWN_HASH465436ce859aa229336ad02524515199MARKDOWNHASH
: the scale bar mark scale, for example, from=2.0, to=3.0, tickinterval=0.25, then the scale is 2.0, 2.25, 2.50, 2.75 and 3.0. (22) to: the end value of the scale bar range value.
(23) toughcolor
: the color of the slot (tough).
(24) variable
: set or get the currently selected scale value, its value type is usually IntVar or StringVar.
(25) width
: for the vertical scale bar this is the width of the slot, for the horizontal scale bar this is the height of the slot.
Tkinter Scale Basic Concepts Example#1
An application that generates horizontal scale bars and vertical scale bars. The scale values range from 0 to 10. The vertical scale bar uses the default length, while the horizontal scale bar is set to 300.
from tkinter import *
window = Tk()
window.title("apidemos.com")
# window.geometry("300x180")
slider1 = Scale(window,from_=0,to=10).pack()
slider2 = Scale(window,from_=0,to=100,
length=300,orient=HORIZONTAL).pack()
window.mainloop()
Output:
Tkinter Scale Basic Concepts Example#2
Set multiple parameters in the Scale() constructor.
from tkinter import *
root = Tk()
root.title("apidemos.com") # Title of the window
slider = Scale(
root,
from_=0, # Starting Point Value
to=20, # Endpoint value
troughcolor="yellow", # Color of the slot
width="30", # Height of the slot
tickinterval=2, # Scale
label="My Scale", # Scale Label
length=300, # Scale Length
orient=HORIZONTAL # Horizontal
)
slider.pack()
root.mainloop()
Output: