是否可以允许用户在Python tkinter中修改spinxbox值?

时间:2015-02-08 13:37:34

标签: python python-3.x tkinter

我打算创建一种计时器,其中将使用spinbox来允许用户调整计时器设置。但是,必须单击箭头按钮来增加/减少值,这将非常烦人。

这是我目前正在处理的代码。

from tkinter import *
import time


class mainmenu():
    def __init__(self, parent,NumDocks):
        self.Container = Frame(parent,width = 64,height = 20)
        self.Container.bind_all("<MouseWheel>", self.onMouseWheel)

        self.Title = Label(self.Container,text = "Kantai Manager",font = ("Arial", '20'))
        self.Title.pack()

        self.Docks = []
        self.DockTimeSelect = []
        for i in range(NumDocks):
            self.Docks.append(Frame(self.Container))
            self.Docks[i].pack()

            CurrentDock = Label(self.Docks[i],text = "Dock {}".format(i+1))
            CurrentDock.pack(side = 'left')

            CurrentDockTimer = Spinbox(self.Docks[i], from_=0,to=60,increment = 1)
            CurrentDockTimer.pack(side='left')

        self.Container.pack()

    def onMousewheel(self,event):
        pass

root = Tk()
root
mainmenu(root,2)
root.mainloop()

我不太清楚要为onMousewheel函数做什么,以及绑定spinbox的命令。

1 个答案:

答案 0 :(得分:0)

最简单的方法是让事件调用按钮按下。您可以编写onMouseWheel函数,如:

def onMouseWheel(self, event):
    if event.delta > 0: # Check if the user scrolls up (positive) or down (negative)
        event.widget.invoke('buttonup')   # Invoke a press of the up button
    else:
        event.widget.invoke('buttondown') # Invoke a press of the down button