我有两个用于两个不同对象(mirror
,beam
)的类。 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
中的坐标不同。
答案 0 :(得分:1)
我会在Html.Raw
内创建Mirror
和Beam
,以便他们可以直接访问GUI
和Canvas
。并且Scale
可以直接访问GUI
和Mirror
,因此我可以将功能分配给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