python-3.x - 如何让滑块更改椭圆的 y 位置而不是其半径
问题描述
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 位置(上下),同时保持椭圆的原始大小。
我不知道该怎么做,我的实现使椭圆形非常小,然后改变了位置。我希望它保持原始大小。这可能吗?
解决方案
那是因为您正在调整椭圆形“中心”的半径而不是 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)
所以像上面的东西......
推荐阅读
- multithreading - 处理同一应用程序的多个线程的有效方法
- java - 使用 morphia 列表非规范化
- android - Android不使用设备宽度和高度创建运行时视图
- c# - ArgumentNullException:参数不能为空 c#(Unity)
- ruby-on-rails - 为 idn-ruby 安装包时出错
- json - Azure 管道映射我想在导入 csv 时在 json 中添加一个静态字段
- c++ - 处理 WM_POINTER 消息时如何检测双击/点击?
- c# - 从 ASP.NET 创建后期绑定 COM 对象时出现 IClassFactory 错误
- c - 只有字母的字符串 - 没有用
- python - 从 JSON 中提取“标题”数据