python-3.x - 如何从在 Python tkinter 中生成按钮的 Scale/Slider 获取值
问题描述
我有一个按钮,单击时会显示滑块/比例。如何从滑块中获取选定的值?我的代码返回错误:“TypeError: funcSlider() missing 1 required positional argument: 'value'”
self.list = {}
def onClick(self):
print (self.list)
return
def funcSlider(self, posX,posY,key,value):
w = tk.Scale(self, from_=0, to=8, orient='horizontal')
w.pack()
w.place(x=posX, y=posY, in_=self)
self.list [key] = value
button = tk.Button(self, text="button ",
command=lambda: self.funcSlider(120,33,'test'),width = 20)
button .pack(anchor="w")
button .place(x=250, y=50, in_=self)
button2 = tk.Button(self, text="Send",
command=self.onClick, width=42,bg="#4BAD2E")
button2 .pack(anchor="w")
button2 .place(x=250, y=430, in_=self)
解决方案
我想我做到了。我不知道这是否是一个“好习惯”,但它有效:D
self.list = {}
def foo(self,x,key):
self.list [key] = x
def onClick(self):
print (self.list)
return
def funcSlider(self, posX,posY,key):
w = tk.Scale(self, from_=0, to=8, orient='horizontal',command=lambda s: self.foo(s,key))
w.pack()
w.place(x=posX, y=posY, in_=self)
self.list [key] = value
button = tk.Button(self, text="button ",
command=lambda: self.funcSlider(120,33,'test'),width = 20)
button .pack(anchor="w")
button .place(x=250, y=50, in_=self)
button2 = tk.Button(self, text="Send",
command=self.onClick, width=42,bg="#4BAD2E")
button2 .pack(anchor="w")
button2 .place(x=250, y=430, in_=self)
推荐阅读
- javascript - 如何附加
- node.js - 这不是指猫鼬模式
- c# - 如何从 Autofac Registrations 中获取用于注册的类型
- node.js - 获取 mongoose 和 expressjs 中填充项目的数量
- telegraf - 如何获取电报的可用输入和输出过滤器列表
- android - 如何将谷歌地图添加到android
- python - 在 python 中使用 selenium 来获取赔率
- json - 如何使用 Python 查询 ReST 存储 API
- spring-batch - Spring Batch:在实体内设置实体
- javascript - Javascript中字符串中的数字数组