简体   繁体   English

如何在Python中为tkinter Scale小部件设置初始值?

[英]How set the initial value for a tkinter Scale widget in Python?

I want to initialize the value of a Scale widget to 2 . 我想将Scale小部件的值初始化为2 How can I do this? 我怎样才能做到这一点? Because I used a grid, .set won't work properly. 由于使用网格, .set将无法正常工作。

Here is my code: 这是我的代码:

Scale(self, command=self.onMove, movesvariable=self.var, from_=1, to=10,orient=HORIZONTAL,length=500,tickinterval=0.5, resolution=0.1, cursor = 'hand2 ').grid(columnspan ='200',rowspan = '4',sticky= W)

Change this to two lines: 将其更改为两行:

yourScale = Scale(self, command=self.onMove, movesvariable=self.var, from_=1, to=10,orient=HORIZONTAL,length=500,tickinterval=0.5, resolution=0.1, cursor = 'hand2 ')
yourScale.grid(columnspan ='200',rowspan = '4',sticky= W)

.grid() returns None ; .grid()返回None calling it after Scale like you've done won't return a Scale object like you would expect, it will return a Nonetype. 像您一样在Scale之后调用它不会像您期望的那样返回Scale对象,它将返回一个Nonetype。 Implementing this small change should allow you to use .set on whatever variable you store the Scale object in. ( yourScale in this example) 实施此小更改,应允许您在存储Scale对象的任何变量上使用.set 。(在此示例中为yourScale

As an aside: You might want to remove the set tag in your question: set references a data structure, not the set function from Tkinter. 顺便说一句:您可能要删除问题中的set标记: set引用数据结构,而不是Tkinter的set函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据Python中另一个tkinter`Scale`小部件的值来控制tkinter`Scale`小部件的值? - How control the value of a tkinter `Scale` widget depending on the value of another tkinter `Scale` widget in Python? 如何在Python中检索tkinter ttk Scale小部件的整数值? - How to retrieve the integer value of tkinter ttk Scale widget in Python? 如何将Tkinter Scale小部件的状态设置为Disabled? - How to set the state of a tkinter Scale widget to disabled? 如何打印tkinter Scale小部件的值? - How to print the value of a tkinter Scale widget? 如何从 tkinter Scale 小部件显示中删除比例值? - How to remove scale value from tkinter Scale widget display? Python/Tkinter:如何将文本小部件内容设置为变量的值? - Python/Tkinter: How to set text widget contents to the value of a variable? 如何将 tkinter Scale 小部件滑块的默认值设置为 100? - How can I set the default value of my tkinter Scale widget slider to 100? 如何通过tkinter Scale小部件的set方法使用文本文件中的值? - How to use value from a text file with the `set` method of a tkinter Scale widget? 如何使用tkinter Scale小部件的值连续更改图像 - How to continuously change an image using the value of a tkinter Scale widget 如何在 tkinter 的文本小部件中设置值? - how to set value in Text widget in tkinter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM