【问题标题】:How to get current spinbox 'from' and 'to' values如何获取当前 spinbox 'from' 和 'to' 值
【发布时间】:2021-05-24 15:40:47
【问题描述】:

如何在函数中获取 spinbox 的值?

sbDays=tk.Spinbox(frame,from_=0,to=366)
sbDays.place(relx=initialX,rely=yDistance)
sbDays.configure(validate='all',validatecommand=(windows.register(validate),'%P'))

def validate(userInput): 
    if userInput=="":
        return True

    try:
        val=int(float(userInput))
    except ValueError:
        return False
    return val>=0 and val<=366

而不是return val&gt;=0 and val&lt;=366。我需要这个:

minVal=spinbox 'from' value of '0'
maxVal=spinbox 'to' value of '366'

return val>=minVal and val<=maxVal

在 C# 中,类似这样:

minVal=this.From()
maxVal=this.To()

【问题讨论】:

    标签: python tkinter spinbox


    【解决方案1】:

    您可以使用cget 方法从小部件中获取属性。
    在这种情况下,您需要minVal = sbDays.cget("from")maxVal = sbDays.cget("to")

    编辑 - 用于多个旋转框
    要将其用于多个旋转框,请将validatecommand 更改为
    validatecommand=(windows.register(validate),'%P', '%W')
    并将validate(userInput) 更改为validate(userInput, widget)。然后用windows.nametowidget(widget)替换我的答案中的sbDays,它应该可以工作。
    validatecommand 中的%W 给出了小部件的名称(来自here),然后用于获取带有nametowidget 的小部件(来自here)。

    【讨论】:

      【解决方案2】:

      您也可以像这样使用.config 方法: sbDays.config("from")sbDays.config("to")。它返回类似('from', 'from', 'From', 0, 0.0)('to', 'to', 'To', 0, 366.0) 的内容。请注意,最后一个值是我们需要的值,因此使用 sbDays.config(...)[-1] 应该会得到所需的结果。

      【讨论】:

        猜你喜欢
        • 2021-05-21
        • 2017-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-02
        • 2017-11-28
        • 1970-01-01
        相关资源
        最近更新 更多