[英]Tkinter: How do I use the value of a scale-widget to change the text of a label-widget?
I'm trying to implement a simple difficulty-slider, so I created a scale-widget.我正在尝试实现一个简单的难度滑块,所以我创建了一个比例小部件。
Using the slider, the user can select the difficulty-setting he wants to play on.使用 slider,用户可以 select 进行他想玩的难度设置。 I also want to have a label-widget that displays the chosen difficulty (Easy, Normal, Hard, usw...)
我还想要一个显示所选难度的标签小部件(简单,普通,困难,usw ...)
So the value of the scale-widget is used to continuously update the text of the label-widget.所以 scale-widget 的值被用来不断更新 label-widget 的文本。 I wrote the following code, but it doesn't seem to work as intended.
我编写了以下代码,但它似乎没有按预期工作。 I think something is wrong with the Adjust_scl-function.
我认为 Adjust_scl 函数有问题。 Could you help me out?
你能帮帮我吗?
I'm quite new to Tk and Python in general.一般来说,我对 Tk 和 Python 很陌生。
from tkinter import *
root = Tk()
root.title("Difficulty")
class Difficulty:
def __init__ (self, master):
global vel_1
vel_1 = IntVar()
self.dif_scale = Scale(master,variable = vel_1,from_ = 3,to = -3,resolution=1, orient=HORIZONTAL, length=450,tickinterval=1, label="Your Dice Roll Modifier",showvalue=0,command=self.Adjust_scl)
self.dif_scale.pack()
self.dif_label = Label(master)
self.dif_label.pack()
def Adjust_scl(self,random_parameter):
if vel_1 == 0:
self.dif_label.config(text="Normal")
elif vel_1 == -1:
self.dif_label.config(text="Challenging")
elif vel_1 == -2:
self.dif_label.config(text="Hard")
elif vel_1 == -3:
self.dif_label.config(text="Insane")
elif vel_1 == 1:
self.dif_label.config(text="Easy")
elif vel_1 == 2:
self.dif_label.config(text="Walk in the Park")
elif vel_1 == 3:
self.dif_label.config(text="Storymode")
dif = Difficulty(root)
root.mainloop()
The Adjust_scl-function refuses to work without the random_parameter, by the way.顺便说一下,Adjust_scl 函数拒绝在没有 random_parameter 的情况下工作。
You need to use get() to get the value from the scale, and you need to fix the indentation of your if/elifs.您需要使用 get() 从刻度中获取值,并且需要修复 if/elifs 的缩进。
def Adjust_scl(self,random_parameter):
if vel_1.get() == 0:
self.dif_label.config(text="Normal")
elif vel_1.get() == -1:
self.dif_label.config(text="Challenging")
elif vel_1.get() == -2:
self.dif_label.config(text="Hard")
elif vel_1.get() == -3:
self.dif_label.config(text="Insane")
elif vel_1.get() == 1:
self.dif_label.config(text="Easy")
elif vel_1.get() == 2:
self.dif_label.config(text="Walk in the Park")
elif vel_1.get() == 3:
self.dif_label.config(text="Storymode")
You might also want to consider putting the text for the levels into a list.您可能还需要考虑将级别的文本放入列表中。
If you did that you wouldn't need the long if/elif code.如果你这样做了,你就不需要长的 if/elif 代码。
Here's the code with a list for the levels, and using self.vel_1 so you don't need to use global.这是带有级别列表的代码,并使用 self.vel_1 所以你不需要使用全局。
from tkinter import *
root = Tk()
root.title("Difficulty")
class Difficulty:
def __init__ (self, master):
self.vel_1 = IntVar()
self.levels = ['Insane', 'Hard', 'Challenging', 'Normal', 'Easy', 'Walk in the Park', 'Story Mode']
self.dif_scale = Scale(master,variable = self.vel_1,from_ = 3,to = -3,resolution=1, orient=HORIZONTAL, length=450,tickinterval=1, label="Your Dice Roll Modifier",showvalue=0,command=self.Adjust_scl)
self.dif_scale.pack()
self.dif_label = Label(master)
self.dif_label.pack()
def Adjust_scl(self,random_parameter):
level = self.vel_1.get()+3
print(level)
self.dif_label.config(text=self.levels[level])
dif = Difficulty(root)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.