对于我的程序,我希望在它旁边有一个水平刻度和一个 Spinbox,它们可以改变彼此的值。例如,如果两者都从 1 变为 100,并且我将 Scale 拖动到 50,我希望旋转框中的数字也更改为 50,反之亦然。这是我的尝试:
class Main_window(ttk.Frame):
"""A program"""
def __init__(self, master):
ttk.Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""Creates all the objects in the window"""
self.scale = ttk.Scale(self, orient = HORIZONTAL, length = 200,
from_ = 1.0, to = 100.0,
command = self.update,
).grid(row = 3,
column = 1,
sticky = W)
spinval = StringVar()
self.spinbox = Spinbox(self, from_ = 1.0, to = 100.0,
textvariable = spinval,
command = self.update,
width = 10).grid(row = 3,
column =3,
sticky = W)
def update(self):
"""Changes the spinbox and the scale according to each other"""
self.scale.set(self.spinbox.get())
self.spinbox.delete(0.0,END)
self.spinbox.insert(0.0,self.scale.get())
def main():
"""Loops the window"""
root = Tk()
root.title("window")
root.geometry("400x300")
app = Main_window(root)
root.mainloop()
main()
首先,当我移动比例滑块时,出现错误:TypeError: update() takes 1 positional argument but 2 were given
其次,当我更改旋转框的值时,我得到: AttributeError: 'NoneType' object has no attribute 'set'
我不知道这是否是正确的方法。欢迎任何帮助。