我正试图让我的GUI中的值可以被程序中的其他类访问,我设法得到concA和concB的值可以被模拟类中的start方法使用但是我不能获取可在粒子类的移动方法中使用的温度值。我用10作为临时值(第133和134行)我希望它是温度/ 100。到目前为止,这是我的代码。
from tkinter import *
import random, math
class master():
def __init__(self):
self.mySim = simulation(self)
self.myGUI = GUI(self)
self.myGUI.menu.mainloop()
def startSim(self, newdata):
self.mySim.start(newdata)
class GUI():
def __init__(self, master):
self.myMaster = master
self.menu = Tk()
self.menu.title("Menu")
self.createMainGUI()
def createMainGUI(self):
#Create Menu
self.catalyst = Checkbutton(self.menu, text = "Catalyst", anchor = 'w', justify = LEFT)
self.temperature = Scale(self.menu, from_=0, to=1000, orient = HORIZONTAL)
self.concA = Scale(self.menu, from_=1, to=50, orient = HORIZONTAL)
self.concB = Scale(self.menu, from_=1, to=50, orient = HORIZONTAL)
self.start = Button(self.menu, text="Start", width=5, height=2)
self.exit = Button(self.menu, text="Exit", width = 5, height=2)
self.title = Label(self.menu, text = "Chemistry reaction model", width = 20, height = 2)
self.option = Label(self.menu, text = "Options:", width = 10, height = 2, anchor = 'w', justify = LEFT)
self.temperaturelabel = Label(self.menu, text = "Temperature in Kelvin:")
self.concALabel = Label(self.menu, text = "Concentration of A:")
self.concBLabel = Label(self.menu, text = "Concentration of B:")
#Format Menu
self.title.grid(column = 1, row = 1, columnspan = 3)
self.option.grid(column = 1, row = 2)
self.catalyst.grid(column = 1, row = 3)
self.temperature.grid(column = 2, row = 4, columnspan = 2)
self.concA.grid(column = 2, row = 5, columnspan = 2)
self.concB.grid(column = 2, row = 6, columnspan = 2)
self.start.grid(column = 1, row = 7)
self.exit.grid(column = 2, row = 7)
self.temperaturelabel.grid(column =1, row = 4)
self.concALabel.grid(column =1, row = 5)
self.concBLabel.grid(column =1, row = 6)
#Event Handlers
self.start.bind('<Button-1>', self.startSimulationGUI)
self.exit.bind('<Button-1>', self.quit)
def update_canvas(self, particles):
self.canvas.delete('all') #clears canvas and redraws all particles at new positions
for i in particles:
self.canvas.create_oval(i.x, i.y, i.x+i.size,i.y+i.size , fill = "red")
def quit(self, event):
self.menu.destroy()
def startSimulationGUI(self, event):
self.simGUI = Toplevel(master=self.menu)
self.simGUI.title("Simulation")
self.canvas = Canvas(self.simGUI)
self.canvas.grid(column = 0, row = 1)
self.canvas.config(width = 400, height=400, bg = "Ghost White")
# (0,0) is the top left and (400, 400) is the bottom right coordinates
self.Toolbar = LabelFrame(self.simGUI, relief = RAISED, width = 200, height = 50, padx = 105)
self.Toolbar.grid(column = 0, row =2)
self.Toolbar.config()
self.MenuExit = Button(self.Toolbar, text="Quit to Menu")
self.MenuExit.grid(row = 0, column = 0)
self.MenuExit.bind('<Button-1>', self.simExit)
self.time = Label(self.Toolbar, text ="Timer:", padx = 30)
self.time.grid(row = 0, column = 1)
#Get Data
newdata = data()
newdata.temp = self.temperature.get()
newdata.catalyst = self.catalyst.getint()
newdata.concA = self.concA.get()
newdata.concB = self.concB.get()
self.myMaster.startSim(newdata)
def simExit(self, event):
self.simGUI.destroy()
self.myMaster.mySim.particles.clear() #makes sure that if the user exits, changes the settings and restarts no extra particles are drawn
class simulation():
def __init__(self, master):
self.particles = []
self.myMaster = master
#self.myParticleData = data()
self.myParticle = particle(0,0)
def start(self, myParticleData):
self.myParticleData = myParticleData
for i in range(0, self.myParticleData.concA):
self.particles.append(particle(random.randint(0,400),random.randint(0,400)))
for i in range(0, self.myParticleData.concB):
self.particles.append(particle(random.randint(0,400),random.randint(0,400)))
self.myMaster.myGUI.update_canvas(self.particles)
self.animate()
def animate(self):
for i in self.particles:
i.walls()
i.move()
self.myMaster.myGUI.update_canvas(self.particles)
self.myMaster.myGUI.menu.after(100, self.animate)
class particle():
def __init__(self, x, y):
self.x = x
self.y = y
#self.tag = a or b or c
self.speed = 0
self.direction = 0
self.myGUI = GUI
self.size = 5
self.myParticleData= data()
#self.hyp = sqrt(self.size**2+self.size**2)
def move(self):
self.direction = random.randint(0, 360)
self.x += math.sin(self.direction) * 10 # USING 10 AS TEMP VALUE
self.y -= math.cos(self.direction) * 10 # USING 10 AS TEMP VALUE
def walls(self):
if self.x > 390 - self.size:
self.x = 2*(400 - self.size) - self.x
self.direction = -self.direction
elif self.x < 10+self.size:
self.x = 2*self.size - self.x
self.direction = - self.direction
if self.y > 390 - self.size:
self.y = 2*(400 - self.size) - self.y
self.direction = 180 - self.direction
elif self.y < 10 + self.size:
self.y = 2*self.size - self.y
self.direction = 180 - self.direction
class data():
def __init__(self):
self.temp = None
self.concA = None
self.concB = None
self.catalyst = None
if __name__ == '__main__':
x = master()
答案 0 :(得分:0)
更改了particle()
类,每个粒子都有自己的温度。更改后跟###
,希望这会有所帮助。
from tkinter import *
import random, math
class master():
def __init__(self):
self.mySim = simulation(self)
self.myGUI = GUI(self)
self.myGUI.menu.mainloop()
def startSim(self, newdata):
self.mySim.start(newdata)
class GUI():
def __init__(self, master):
self.myMaster = master
self.menu = Tk()
self.menu.title("Menu")
self.createMainGUI()
def createMainGUI(self):
#Create Menu
self.catalyst = Checkbutton(self.menu, text = "Catalyst", anchor = 'w', justify = LEFT)
self.temperature = Scale(self.menu, from_=0, to=1000, orient = HORIZONTAL)
self.concA = Scale(self.menu, from_=1, to=50, orient = HORIZONTAL)
self.concB = Scale(self.menu, from_=1, to=50, orient = HORIZONTAL)
self.start = Button(self.menu, text="Start", width=5, height=2)
self.exit = Button(self.menu, text="Exit", width = 5, height=2)
self.title = Label(self.menu, text = "Chemistry reaction model", width = 20, height = 2)
self.option = Label(self.menu, text = "Options:", width = 10, height = 2, anchor = 'w', justify = LEFT)
self.temperaturelabel = Label(self.menu, text = "Temperature in Kelvin:")
self.concALabel = Label(self.menu, text = "Concentration of A:")
self.concBLabel = Label(self.menu, text = "Concentration of B:")
#Format Menu
self.title.grid(column = 1, row = 1, columnspan = 3)
self.option.grid(column = 1, row = 2)
self.catalyst.grid(column = 1, row = 3)
self.temperature.grid(column = 2, row = 4, columnspan = 2)
self.concA.grid(column = 2, row = 5, columnspan = 2)
self.concB.grid(column = 2, row = 6, columnspan = 2)
self.start.grid(column = 1, row = 7)
self.exit.grid(column = 2, row = 7)
self.temperaturelabel.grid(column =1, row = 4)
self.concALabel.grid(column =1, row = 5)
self.concBLabel.grid(column =1, row = 6)
#Event Handlers
self.start.bind('<Button-1>', self.startSimulationGUI)
self.exit.bind('<Button-1>', self.quit)
def update_canvas(self, particles):
self.canvas.delete('all') #clears canvas and redraws all particles at new positions
for i in particles:
self.canvas.create_oval(i.x, i.y, i.x+i.size,i.y+i.size , fill = "red")
def quit(self, event):
self.menu.destroy()
def startSimulationGUI(self, event):
self.simGUI = Toplevel(master=self.menu)
self.simGUI.title("Simulation")
self.canvas = Canvas(self.simGUI)
self.canvas.grid(column = 0, row = 1)
self.canvas.config(width = 400, height=400, bg = "Ghost White")
# (0,0) is the top left and (400, 400) is the bottom right coordinates
self.Toolbar = LabelFrame(self.simGUI, relief = RAISED, width = 200, height = 50, padx = 105)
self.Toolbar.grid(column = 0, row =2)
self.Toolbar.config()
self.MenuExit = Button(self.Toolbar, text="Quit to Menu")
self.MenuExit.grid(row = 0, column = 0)
self.MenuExit.bind('<Button-1>', self.simExit)
self.time = Label(self.Toolbar, text ="Timer:", padx = 30)
self.time.grid(row = 0, column = 1)
#Get Data
newdata = data()
newdata.temp = self.temperature.get()
newdata.catalyst = self.catalyst.getint()
newdata.concA = self.concA.get()
newdata.concB = self.concB.get()
newdata.temperature = self.temperature.get() ###
self.myMaster.startSim(newdata)
def simExit(self, event):
self.simGUI.destroy()
self.myMaster.mySim.particles.clear() #makes sure that if the user exits, changes the settings and restarts no extra particles are drawn
class simulation():
def __init__(self, master):
self.particles = []
self.myMaster = master
#self.myParticleData = data()
#self.myParticle = particle(0,0) ###
def start(self, myParticleData):
self.myParticleData = myParticleData
for i in range(0, self.myParticleData.concA):
self.particles.append(particle(random.randint(0,400),random.randint(0,400),self.myParticleData.temperature)) ###
for i in range(0, self.myParticleData.concB):
self.particles.append(particle(random.randint(0,400),random.randint(0,400),self.myParticleData.temperature)) ###
self.myMaster.myGUI.update_canvas(self.particles)
self.animate()
def animate(self):
for i in self.particles:
i.walls()
i.move()
self.myMaster.myGUI.update_canvas(self.particles)
self.myMaster.myGUI.menu.after(100, self.animate)
class particle():
def __init__(self, x, y, temp): ###
self.x = x
self.y = y
self.temp = temp ###
#self.tag = a or b or c
self.speed = 0
self.direction = 0
self.myGUI = GUI
self.size = 5
self.myParticleData= data()
#self.hyp = sqrt(self.size**2+self.size**2)
def move(self):
self.direction = random.randint(0, 360)
self.x += math.sin(self.direction) * .1 * self.temp ###
self.y -= math.cos(self.direction) * .1 * self.temp ###
def walls(self):
if self.x > 390 - self.size:
self.x = 2*(400 - self.size) - self.x
self.direction = -self.direction
elif self.x < 10+self.size:
self.x = 2*self.size - self.x
self.direction = - self.direction
if self.y > 390 - self.size:
self.y = 2*(400 - self.size) - self.y
self.direction = 180 - self.direction
elif self.y < 10 + self.size:
self.y = 2*self.size - self.y
self.direction = 180 - self.direction
class data():
def __init__(self):
self.temp = None
self.concA = None
self.concB = None
self.catalyst = None
if __name__ == '__main__':
x = master()