Python 如何在Tkinter中固定滑块?
我一直在尝试为椭圆形制作一个左、右和缩放滑块,但我无法让它们同步工作。它们自己工作,但当我尝试在混合中添加第二个滑块时,它们只是重置其位置和比例。我使用Python 如何在Tkinter中固定滑块?,python,python-3.x,tkinter,tkinter-canvas,tkinter-scale,Python,Python 3.x,Tkinter,Tkinter Canvas,Tkinter Scale,我一直在尝试为椭圆形制作一个左、右和缩放滑块,但我无法让它们同步工作。它们自己工作,但当我尝试在混合中添加第二个滑块时,它们只是重置其位置和比例。我使用repl.it来编写代码 这是我的密码: import tkinter root = tkinter.Tk() root.option_add('*Font', 'Times') ##### MODEL ###### y_intvar = tkinter.IntVar() #create special tk variabl
repl.it
来编写代码
这是我的密码:
import tkinter
root = tkinter.Tk()
root.option_add('*Font', 'Times')
##### MODEL ######
y_intvar = tkinter.IntVar() #create special tk variable
y_intvar.set(150) #set initial value
x_intvar = tkinter.IntVar()
x_intvar.set(150)
radius_intvar = tkinter.IntVar()
radius_intvar.set(150)
r = 150 #fixed value for radius
x = 150 #fixed value for the x position
###### CONTROLLER #########
# Event handler for slider
def y_changed(new_intval):
#read the value of y slider
y = y_intvar.get()
# update the view
canvas.coords(circle_item, x-r, y-r, x+r, y+r)
def x_changed(new_intval):
#read the value of y slider
x = x_intvar.get()
# update the view
canvas.coords(circle_item, x-r, y-r, x+r, y+r)
def radius_changed(new_intval):
# Get data from model
# Could do this: r = int(new_intval)
r = radius_intvar.get()
# Controller updating the view
canvas.coords(circle_item, x-r, y-r, x+r, y+r)
#create the slider
Y_slider = tkinter.Scale(root, from_=1, to=300, variable=y_intvar, label='y coordinate', command=y_changed, background='#FF6666')
X_slider = tkinter.Scale(root, from_=1, to=300, variable=x_intvar, label='x coordinate', command=x_changed, background='#FFFF33')
radius_slider = tkinter.Scale(root, from_=1, to=300, variable=radius_intvar,label='Radius ', command=radius_changed, background='#66FF66')
#place the slider in row1 and column0
Y_slider.grid(row=2, column=0, sticky=tkinter.W)
X_slider.grid(row=3, column=0, sticky=tkinter.W)
radius_slider.grid(row=1, column=0, sticky=tkinter.W)
#directions for the user
text = tkinter.Label(root, text='Drag slider \n to adjust \n circle.')
text.grid(row=0, column=0)
###### VIEW ################### present the information to user
#need a canvas to draw the circle on
canvas = tkinter.Canvas(root, width=500, height=500, background='#FFFFFF')
canvas.grid(row=0, rowspan=9, column=1)
# Create a circle on the canvas
y = y_intvar.get()
x = x_intvar.get()
r = radius_intvar.get()
circle_item = canvas.create_oval(x-r, y-r, x+r, y+r,outline='#000000', fill='#00FFFF')
root.mainloop()```
您未将
x
、y
和r
声明为全局内部x\u已更改()
、y\u已更改()
和radius\u已更改()
:
实际上,您可以将这三个函数合并为一个函数,而不需要将这三个变量声明为全局变量:
def on_changed(_):
x = x_intvar.get()
y = y_intvar.get()
r = radius_intvar.get()
canvas.coords(circle_item, x-r, y-r, x+r, y+r)
#create the slider
Y_slider = tkinter.Scale(root, from_=1, to=300, variable=y_intvar, label='y coordinate', command=on_changed, background='#FF6666')
X_slider = tkinter.Scale(root, from_=1, to=300, variable=x_intvar, label='x coordinate', command=on_changed, background='#FFFF33')
radius_slider = tkinter.Scale(root, from_=1, to=300, variable=radius_intvar,label='Radius ', command=on_changed, background='#66FF66')
def on_changed(_):
x = x_intvar.get()
y = y_intvar.get()
r = radius_intvar.get()
canvas.coords(circle_item, x-r, y-r, x+r, y+r)
#create the slider
Y_slider = tkinter.Scale(root, from_=1, to=300, variable=y_intvar, label='y coordinate', command=on_changed, background='#FF6666')
X_slider = tkinter.Scale(root, from_=1, to=300, variable=x_intvar, label='x coordinate', command=on_changed, background='#FFFF33')
radius_slider = tkinter.Scale(root, from_=1, to=300, variable=radius_intvar,label='Radius ', command=on_changed, background='#66FF66')