简体   繁体   中英

How to change the side where the current value of the tkinter Scale widget is being displayed?

I am using a tkinter Scale widget and I have two scales next to each other. I want the numbers on one of them to go to the other side.

This is the code I am using:

#IMPORTS
from tkinter import *
import time

#USER INPUT
user = input("Please Enter Your Username")
time.sleep(0.4)
pass_ = imput("Please Enter Your Password")
time.sleep(0.6)              

#DEFINITIONS

#STUFF
master = Tk()

#LEFT
left = Scale(master,from_=0,to=249,length=550,width=25,tickinterval=152,sliderlength=30)
left.pack()
left.set(152)
left.grid(row=0, column=3)

#RIGHT
right = Scale(master,from_=0,to=249,length=550,width=25,tickinterval=152,sliderlength=30)
right.pack()
right.set(152)
right.grid(row=0, column=6)

#BUTTONS
send = Button(master,text="Send To Bot",command=send)
send.pack()
send.grid(row=15, column=4)

#RUN CANVAS
mainloop()

I want the right scale to have the numbers on the right side and the left scale to have the numbers on the left side (set by default).

(By the way this coding isn't finished which is why the definitions aren't done.)

I don't think there's a way to specify to the widget which side the number appears on. However, you can customize the widget's showvalue attribute to turn off the default label, and assign an IntVar() to the scale's variable attribute to monitor the value of the slider externally, and update a Label widget that you can place anywhere as the value of the slider changes:

master = Tk()

leftValue = IntVar()  # IntVars to hold
rightValue = IntVar() # values of scales

leftScale = Scale(master, from_=0, to=249, variable=leftValue, showvalue=0)
leftScale.set(152)

rightScale = Scale(master, from_=0, to=249, variable=rightValue, showvalue=0)
rightScale.set(152)

leftLabel = Label(master, textvariable=leftValue)   # labels that will update
rightLabel = Label(master, textvariable=rightValue) # with IntVars as slider moves

leftLabel.grid(row=0, column=0)
leftScale.grid(row=0, column=1)
rightLabel.grid(row=0, column=3)
rightScale.grid(row=0, column=2)

mainloop()

Of course, the labels won't move as the slider changes, but if you really need that functionality, you could look into creating a subclass of Frame, and finding a way to change the geometry of the Label as the slider's value changes.

Edit: Just as a note, you shouldn't mix pack and grid in your code. All of those widgets should be handled by just one geometry manager.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM