是否可以根据其他类的事件来更新对象?

时间:2019-07-19 16:50:41

标签: python-3.x tkinter

我有两个用于两个不同对象(mirrorbeam)的类。 mirror是根据滑块/比例绘制的,而beam最初是根据mirror的属性绘制的,并根据其他mirror属性进行计算和更新。我找到了一种在滑块更改时重绘mirror的方法,但没有重绘beam的方法。我该怎么办?

mirror在创建时传递给滑块,然后创建一个属性(self.distSlider = distSlider)。我尝试将整个mirror传递给beam(因为从属属性需要mirror的其他某些特性),并且还从此滑块创建了特性,但是从{ {1}}覆盖beam中的更新事件。仅将滑块传递到两个对象具有相同的结果。

mirror

为了避免代码段中出现更多膨胀,我省略了Beam类中的任何更新尝试,但实际上只是将from tkinter import * def drawCoords(point, distance): mirrorX = distance; mirrorY = point.y; mirrorLen = 50; return [mirrorX, mirrorY - mirrorLen, mirrorX, mirrorY + mirrorLen], mirrorLen; def midpoint(coords): return Point(round((coords[2]+coords[0])/2),round((coords[3]+coords[1])/2)); class GUI: def __init__(self,master): frame = Frame(master) frame.pack(fill = BOTH, expand = 1) bg = Canvas(frame, width = 400, height = 400, background = "#F0F0F0", borderwidth = 3, relief = SUNKEN) bg.pack(fill = BOTH, expand = 1) dist = Scale(frame, from_ = 100, to = 200, orient=HORIZONTAL) dist.pack(fill = X, expand = 1) class Mirror: def __init__(self, canvas, distSlider, point): self.canvas = canvas self.distSlider = distSlider distSlider.configure(command=self.updateDistEvent) self.distance = distSlider.get() self.point = point self.coords, self.length = drawCoords(self.point, self.distance) self.midpoint = midpoint(self.coords) self.id = canvas.create_line(self.coords, width = 2) def updateDistEvent(self, event): newDistance = self.distSlider.get() self.draw(newDistance) def draw(self, newDistance): self.canvas.delete(self.id) self.distance = newDistance self.coords, self.length = drawCoords(self.point, self.distance) self.midpoint = midpoint(self.coords) self.id = self.canvas.create_line(self.coords, width = 2) pass class Point: def __init__(self, initx, inity): self.x = initx self.y = inity class Beam: def __init__(self, canvas, start, end, endObj): self.canvas = canvas self.start = start self.end = end self.id = self.canvas.create_line(self.start.x, self.start.y, self.end.x, self.end.y) def draw(self): pass root = Tk() gui_r = GUI(root) root.update() mainCanvas = root.children['!frame'].children['!canvas']; distSlider = root.children['!frame'].children['!scale']; center = Point(round(mainCanvas.winfo_width()/2),round(mainCanvas.winfo_height()/2)) mirror = Mirror(mainCanvas, distSlider, center) source = Point(0, center.y) beam = Beam(mainCanvas, source, mirror.midpoint, mirror) root.mainloop() 对象的参数传递给mirror,然后进行相同操作与Mirror类中的方法类似,beam中的坐标不同。

1 个答案:

答案 0 :(得分:1)

我会在Html.Raw内创建MirrorBeam,以便他们可以直接访问GUICanvas。并且Scale可以直接访问GUIMirror,因此我可以将功能分配给Beam,后者可以访问Scale进行更改,并获得其其他功能attribiut,并在Mirror中使用它。

也许Beam并不直接依赖Beam,但是创建起来更容易。

Mirror

其他方法只能

  • from tkinter import * def drawCoords(point, distance): mirrorX = distance mirrorY = point.y mirrorLen = 50; return [mirrorX, mirrorY - mirrorLen, mirrorX, mirrorY + mirrorLen], mirrorLen; def midpoint(coords): return Point(round((coords[2]+coords[0])/2),round((coords[3]+coords[1])/2)); class GUI: def __init__(self,master): frame = Frame(master) frame.pack(fill = BOTH, expand = 1) self.bg = Canvas(frame, width = 400, height = 400, background = "#F0F0F0", borderwidth = 3, relief = SUNKEN) self.bg.pack(fill = BOTH, expand = 1) self.dist = Scale(frame, from_ = 100, to = 200, orient=HORIZONTAL) self.dist.pack(fill = X, expand = 1) self.dist.configure(command=self.update_figures) root.update() self.center = Point(round(self.bg.winfo_width()/2),round(self.bg.winfo_height()/2)) self.mirror = Mirror(self.bg, self.dist.get(), self.center) self.source = Point(0, self.center.y) self.beam = Beam(self.bg, self.source, self.mirror.midpoint, self.mirror) def update_figures(self, event): self.mirror.draw(self.dist.get()) self.beam.draw(self.mirror.midpoint) class Mirror: def __init__(self, canvas, distance, point): self.canvas = canvas self.distance = distance self.point = point self.coords, self.length = drawCoords(self.point, self.distance) self.midpoint = midpoint(self.coords) self.id = canvas.create_line(self.coords, width=2) def draw(self, distance): self.distance = distance self.coords, self.length = drawCoords(self.point, self.distance) self.midpoint = midpoint(self.coords) # move in new place without deleting self.canvas.coords(self.id, self.coords) class Point: def __init__(self, initx, inity): self.x = initx self.y = inity class Beam: def __init__(self, canvas, start, end, endObj): self.canvas = canvas self.start = start self.end = end self.id = self.canvas.create_line(self.start.x, self.start.y, self.end.x, self.end.y) def draw(self, end): self.end = end # move in new place without deleting self.canvas.coords(self.id, (self.start.x, self.start.y, self.end.x, self.end.y)) root = Tk() gui_r = GUI(root) root.mainloop() 放入Mirror中,以便GUI可以更改Mirror
  • Scale放入Beam中,以便Mirror可以在更改后更改Mirror