Python3:Tkinter和buggy scale

时间:2017-01-13 19:26:25

标签: python-3.x tkinter

对于我正在研究的项目,我需要为用户创建一种方式来指定一系列数字但是该范围有意义(即,没有最小值高于最大值),为此我创建了"双滑块",其中1的值影响另一个的范围(反之亦然)。

这很好用(下面的代码)然而当我有两组双滑块时我会得到一些奇怪的行为(让我们称之为X和Y为简洁),例如当我将XLower的值提高到10以上时放手它会减少到10,同样下面的两个滑块似乎相互作用。

双滑块的代码:

        self.varHPLower = tk.IntVar()
        self.varHPLower = 0
        self.varHPUpper = tk.IntVar()
        self.varHPUpper = 250
        self.sclHPLower = tk.Scale(self, command=self.updatedHPLowerScale, variable=self.varHPLower, from_=self.varHPLower, to=self.varHPUpper, orient=tk.HORIZONTAL, resolution=10, bg='#ffef82', width=10)
        self.sclHPLower.place(x=100, y=320, width=570, height=40)
        self.sclHPLower.set(self.varHPLower)
        self.sclHPUpper = tk.Scale(self, command=self.updatedHPUpperScale, variable=self.varHPUpper, from_=self.varHPLower, to=self.varHPUpper, orient=tk.HORIZONTAL, resolution=10, bg='#ffef82', width=10)
        self.sclHPUpper.place(x=100, y=360, width=570, height=40)
        self.sclHPUpper.set(self.varHPUpper)

        self.lblHPLowerLeftLabel = tk.Label(self, text=str(self.varHPLower), anchor='w', bg='#ffef82')
        self.lblHPLowerLeftLabel.place(x=75, y=330, width=25, height=20)
        self.lblHPLowerRightLabel = tk.Label(self, text=str(self.varHPUpper), anchor='w', bg='#ffef82')
        self.lblHPLowerRightLabel.place(x=670, y=330, width=25, height=20)
        self.lblHPUpperLeftLabel = tk.Label(self, text=str(self.varHPLower), anchor='w', bg='#ffef82')
        self.lblHPUpperLeftLabel.place(x=75, y=370, width=25, height=20)
        self.lblHPUpperRightLabel = tk.Label(self, text=str(self.varHPUpper), anchor='w', bg='#ffef82')
        self.lblHPUpperRightLabel.place(x=670, y=370, width=25, height=20)
        self.lblHPRange = tk.Label(self, text='', bg='#ffef82', font=('Lucida Grande',12))
        self.lblHPRange.place(x=350, y=285, width=200, height=30)

        self.varRetreatCostLower = tk.IntVar()
        self.varRetreatCostLower = 0
        self.varRetreatCostUpper = tk.IntVar()
        self.varRetreatCostUpper = 5
        self.sclRetreatCostLower = tk.Scale(self, command=self.updatedRetreatCostLowerScale, variable=self.varRetreatCostLower, from_=self.varRetreatCostLower, to=self.varRetreatCostUpper, orient=tk.HORIZONTAL, bg='#ffef82', width=10)
        self.sclRetreatCostLower.place(x=730, y=370, width=570, height=40)
        self.sclRetreatCostLower.set(self.varRetreatCostLower)
        self.sclRetreatCostUpper = tk.Scale(self, command=self.updatedRetreatCostUpperScale, variable=self.varRetreatCostUpper, from_=self.varRetreatCostLower, to=self.varRetreatCostUpper, orient=tk.HORIZONTAL, bg='#ffef82', width=10)
        self.sclRetreatCostUpper.place(x=730, y=410, width=570, height=40)
        self.sclRetreatCostUpper.set(self.varRetreatCostUpper)

        self.lblRetreatCostLowerLeftLabel = tk.Label(self, text=str(self.varRetreatCostLower), anchor='w', bg='#ffef82')
        self.lblRetreatCostLowerLeftLabel.place(x=705, y=380, width=25, height=20)
        self.lblRetreatCostLowerRightLabel = tk.Label(self, text=str(self.varRetreatCostUpper), anchor='w', bg='#ffef82')
        self.lblRetreatCostLowerRightLabel.place(x=1300, y=380, width=25, height=20)
        self.lblRetreatCostUpperLeftLabel = tk.Label(self, text=str(self.varRetreatCostLower), anchor='w', bg='#ffef82')
        self.lblRetreatCostUpperLeftLabel.place(x=705, y=420, width=25, height=20)
        self.lblRetreatCostUpperRightLabel = tk.Label(self, text=str(self.varRetreatCostUpper), anchor='w', bg='#ffef82')
        self.lblRetreatCostUpperRightLabel.place(x=1300, y=420, width=25, height=20)
        self.lblRetreatCostRange = tk.Label(self, text='', bg='#ffef82',font=('Lucida Grande',12))
        self.lblRetreatCostRange.place(x=980, y=335, width=200, height=30)

def updatedHPLowerScale(self,* args,** kwargs):
    self.sclHPUpper.config(from_=self.sclHPLower.get())
    self.lblHPUpperLeftLabel.config(text=str(self.sclHPLower.get()))
    self.updateHPRangeLabel()

def updatedHPUpperScale(self,* args,** kwargs):
    self.sclHPLower.config(to=self.sclHPUpper.get())
    self.lblHPLowerRightLabel.config(text=str(self.sclHPUpper.get()))
    self.updateHPRangeLabel()

def updateHPRangeLabel(self):
    if self.sclHPLower.get() == self.sclHPUpper.get():
        self.lblHPRange.config(text='HP Range: {}'.format(self.sclHPUpper.get()))
    else:
        self.lblHPRange.config(text='HP Range: {} to {}'.format(self.sclHPLower.get(), self.sclHPUpper.get()))

def updatedRetreatCostLowerScale(self,* args,** kwargs):
    self.sclRetreatCostUpper.config(from_=self.sclRetreatCostLower.get())
    self.lblRetreatCostUpperLeftLabel.config(text=str(self.sclRetreatCostLower.get()))
    self.updateRetreatCostRangeLabel()

def updatedRetreatCostUpperScale(self,* args,** kwargs):
    self.sclRetreatCostLower.config(to=self.sclRetreatCostUpper.get())
    self.lblRetreatCostLowerRightLabel.config(text=str(self.sclRetreatCostUpper.get()))
    self.updateRetreatCostRangeLabel()

def updateRetreatCostRangeLabel(self):
    if self.sclRetreatCostLower.get() == self.sclRetreatCostUpper.get():
        self.lblRetreatCostRange.config(text='RetreatCost Range: {}'.format(self.sclRetreatCostUpper.get()))
    else:
        self.lblRetreatCostRange.config(text='RetreatCost Range: {} to {}'.format(self.sclRetreatCostLower.get(), self.sclRetreatCostUpper.get()))

正如我所说,其中有两个(每个都有自己的方法),我可能只是错过了一个名不副实的变量,但是如果你能帮助追踪那些烦恼的来源我会非常感激!

1 个答案:

答案 0 :(得分:2)

    self.varHPLower = tk.IntVar()
    self.varHPLower = 0

因此,您将丢弃您创建的IntVar,将其替换为整数 - 这些整数无效作为Slider的variable=选项。您必须使用IntVars的.get().set()方法对其存储值进行操作。