简体   繁体   English

Tkinter:如何使用 scale-widget 的值来更改 label-widget 的文本?

[英]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.

相关问题 如何使用tkinter Scale小部件滚动显示长文本标签? - How to scroll through a long text label with tkinter Scale widget? 如何使用tkinter Scale小部件的值连续更改图像 - How to continuously change an image using the value of a tkinter Scale widget 我们如何根据订阅的主题消息的变化动态更改 Tkinter 的标签小部件中的文本? - How do we dynamically change the text in label widget of Tkinter according to the change of subscribed topic message? 如何通过tkinter Scale小部件的set方法使用文本文件中的值? - How to use value from a text file with the `set` method of a tkinter Scale widget? 如何使用 Tkinter 菜单小部件? - How do I use the The Tkinter Menu Widget? 如何打印tkinter Scale小部件的值? - How to print the value of a tkinter Scale widget? Tkinter,Python:如何保存在“输入”小部件中输入的文本? 如何移动标签? - Tkinter, Python: How do I save text entered in the Entry widget? How do I move a label? 如何根据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? 如何通过添加百分比符号来自定义Tkinter Scale小部件的标签 - How to customize the label of a tkinter Scale widget by adding a percentage sign Python / Tkinter:如何使用Entry小部件更改Label小部件? - Python/Tkinter: How to change Label widget with using Entry widget?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM