ttk s递增1

2024-10-04 01:24:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是我有一个刻度和一个旋转框,它们可以改变彼此的值。例如,如果两者都从1到100,如果我将比例设置为50,则数字调整框也将更改,反之亦然。现在除了一个小问题外,我已经让它运行得很好了。我没法让ttk量表按整数递增。每次我改变刻度,我的数字后面都有一大堆小数。这是我的代码:

def create_widgets(self):
"""my widgets"""
    spinval = IntVar()

    self.scale = ttk.Scale(self, orient = HORIZONTAL,
                                   length = 200,
                                   from_ = 1, to = 100,
                                   variable = spinval)
    self.scale.grid(row = 3,column = 1,sticky = W)


    self.spinbox = Spinbox(self, from_ = 1, to = 100,
                                   textvariable = spinval,
                                   command = self.update,
                                   width = 10)
    self.spinbox.grid(row = 3,column =3,sticky = W)

def update(self, nothing):
    """Updates the scale and spinbox"""
    self.scale.set(self.spinbox.get())

现在我的问题是:有没有可能使它以整数递增,或者改变正常的Tkinter比例尺的图形,使它看起来更好。欢迎任何帮助。在


Tags: tofromselfdefcolumn数字整数widgets
1条回答
网友
1楼 · 发布于 2024-10-04 01:24:37
def create_widgets(self):
    """my widgets"""
    spinval = IntVar()

    self.scale = ttk.Scale(self, orient=HORIZONTAL,
                                length=200,
                                from_=1, to=100,
                                variable=spinval,
                                command=self.accept_whole_number_only)
    self.scale.grid(row=3, column=1, sticky=W)


    self.spinbox = Spinbox(self, from_=1, to=100,
                                textvariable=spinval,
                                command=self.update,
                                width=10)
    self.spinbox.grid(row=3,column=3, sticky=W)

def accept_whole_number_only(self, e=None):
    value = self.scale.get()
    if int(value) != value:
        self.scale.set(round(value))

def update(self, e=None):
    """Updates the scale and spinbox"""
    self.scale.set(self.spinbox.get())

相关问题 更多 >