【问题标题】:How to get the slider to change the y-location of the oval instead of its radius如何让滑块更改椭圆的 y 位置而不是其半径
【发布时间】:2018-05-25 02:20:26
【问题描述】:
import tkinter #often people import tkinter as *

#####
# Create root window 
####
root = tkinter.Tk()

#####
# Create Model
######
radius_intvar = tkinter.IntVar()
radius_intvar.set(100) #initialize radius
# center of circle
x = 150 
y = 150

######
# Create Controller
#######
# Event handler for slider
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)
# Instantiate and place slider
radius_slider = tkinter.Scale(root, from_=1, to=150, variable=radius_intvar,    
                              label='Radius', command=radius_changed)
radius_slider.grid(row=1, column=0, sticky=tkinter.W)
# Create and place directions for the user
text = tkinter.Label(root, text='Drag slider \nto adjust\ncircle.')
text.grid(row=0, column=0)

######
# Create View
#######
# Create and place a canvas
canvas = tkinter.Canvas(root, width=300, height=300, background='#FFFFFF')
canvas.grid(row=0, rowspan=2, column=1)

# Create a circle on the canvas to match the initial model
r = radius_intvar.get()
circle_item = canvas.create_oval(x-r, y-r, x+r, y+r, 
                                 outline='#000000', fill='#00FFFF')
#######
# Event Loop
#######
root.mainloop()

此代码目前只是更改椭圆的半径。如何让滑块改变椭圆的 y 位置(上下),同时保持椭圆的原始大小。

我不知道该怎么做,我的实现使椭圆形非常小,然后更改了位置。我希望它保持原始大小。这可能吗?

【问题讨论】:

    标签: python-3.x tkinter tk tkinter-canvas


    【解决方案1】:

    这是因为您正在调整椭圆的“中心”的半径而不是 y 值。

    # Create Model
    ######
    ##radius_intvar = tkinter.IntVar()
    radius_intvar.set(100) #initialize radius
    yNew_intvar = tkinter.IntVar()
    yNew_intvar.set(150)  ##initialize y-location
    # center of circle
    x = 150 
    y = 150
    
    yNew = yNew_intvar.get()
    # Controller updating the view
    canvas.coords(circle_item, x, y-yNew, x, y+yNew)
    # Instantiate and place slider
    yNew_slider = tkinter.Scale(root, from_=1, to=150, variable=yNew_intvar,    
                                  label='Y-Value', command=yNew_changed)
    yNew_slider.grid(row=1, column=0, sticky=tkinter.W)
    

    所以像上面这样......

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 2014-07-07
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 2023-02-06
      • 2014-06-12
      • 1970-01-01
      相关资源
      最近更新 更多