我对python比较陌生,但是我确实有相当的Java经验。但是,将UI信息集成到我的代码中一直是我的痛点。现在,我正在开发一个程序,该程序需要从UI中获取输入数据(称为GUI.py的程序)并将其合并到处理该数据的程序中(称为multpleScattering.py的程序)。任何帮助将不胜感激!我现在正在踩水哈哈
我在使用正确的线程方面遇到麻烦,并且更普遍地讲,无法将UI有效地集成到处理程序中。 P.S:我需要GUI.py的帮助,而只是multpleScattering.py的开始,无需尝试了解过去的def ShootMuons():。 P.S.S:在示例代码下可以找到我运行代码时发生的情况的描述。
from tkinter import *
class GUI:
def __init__(self, length, initX, initY, initPhi, delX, delY, delPhi,
initSimSpeed, isProgramOn):
self.length, self.initX, self.initX, self.initY = length,
initX, initY, initPhi
self.delX, self.delY, self.delPhi = delX, delY, delPhi
self.initSimSpeed = initSimSpeed
self.isProgramOn = isProgramOn
def setUpGUI(self):
tb1 = DoubleVar()
tb2 = IntVar()
tb3 = IntVar()
tb4 = IntVar()
tb5 = DoubleVar()
tb6 = IntVar()
tb7 = IntVar()
tb8 = DoubleVar()
window = Tk()
window.title("GUI")
window.geometry("800x200")
# Simulation Speed
Label(window,text="Simulation Speed").grid(row=0,column=0)
Entry(window, textvariable = tb1).grid(row=0,column=1)
# Length
Label(window,text="Length").grid(row=0,column=3)
Entry(window, textvariable = tb2).grid(row=0,column=4)
# Init Pos
Label(window,text="InitX").grid(row=1,column=0)
Entry(window, textvariable=tb3).grid(row=1,column=1)
Label(window,text="InitY").grid(row=1,column=2)
Entry(window, textvariable=tb4).grid(row=1,column=3)
Label(window,text="InitPhi").grid(row=1,column=4)
Entry(window, textvariable=tb5).grid(row=1,column=5)
# Del Pos
Label(window,text="delX").grid(row=2,column=0)
Entry(window, textvariable=tb6).grid(row=2,column=1)
Label(window,text="delY").grid(row=2,column=2)
Entry(window, textvariable=tb7).grid(row=2,column=3)
Label(window,text="delPhi").grid(row=2,column=4)
Entry(window, textvariable=tb8).grid(row=2,column=5)
# Start Simulation Button
StartBtn = Button(window,text="Start SIM", command=lambda:
StartIsPressed()).grid(row=3,column=0)
#Command for Start Button
def StartIsPressed(self):
print("Pressed")
self.initSimSpeed = tb1.get()
self.length = tb2.get()
self.initX = int(tb3.get())
self.initY = int(tb4.get())
self.initPhi = int(tb5.get())
self.delX = int(tb6.get())
self.delY = int(tb7.get())
self.delPhi = int(tb7.get())
print(self.initSimSpeed)
print(self.initX)
self.isProgramOn = True
from numba import jit
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import math
import random
from geometry import *
from constants import *
from iterateMuon import iterateMuon
from circleIntersectLine import circleIntersectLine
from propagateMuon import propagateMuon
from chamber import chamber
import numpy as np
import pickle
import threading
import time
from GUI import GUI
random.seed(1.0)
#Setting up Plot
sub1 = plt.subplot(212)
sub1.margins(0.05) # Default margin is 0.05, value 0 means fit
sub1.set_xlim([0, 100])
sub1.set_ylim([-200, 200])
sub2 = plt.subplot(221)
sub2.set_title('Fitness')
sub2.set_xlim([0, 40])
sub3 = plt.subplot(222)
sub3.set_title('y Residual')
residualLeavingBounds = []
#Initiating data variables
designLength, designX, designY, designPhi = 0,0,0,0
deltaX, deltaY, deltaPhi = 0,0,0
initSimSpeed = 0
programIsOn = False
#getting data from GUI
while not(programIsOn):
gui = GUI(designLength, designX, designY, designPhi, deltaX, deltaY, deltaPhi, initSimSpeed, programIsOn)
gui.setUpGUI()
def main():
thread = threading.Thread(target=gui.setUpGUI)
thread.start()
thread.join()
chamber1 = chamber(1, designLength, designPhi, designX, designY,
designPhi+deltaPhi, designX+deltaX, designY+deltaY)
chamber1.plotChamber(sub1,sub2,sub3)
def shootMuons(chamber1, simSpeed):
for i in range(nEvents):
if i%1000==0: print(i*1.0/nEvents)
#set up inital state of muon
angleInitial = 1
speedInitial = 1000
angleInitial = random.random()-.5
charge = random.random()
if charge > .5: charge = 1
else: charge = -1
xInitial = 0
yInitial = 0
#create muon track
if verbose > 5: print("start angle {} speed {} charge
{} x {} y {}".format( angleInitial, speedInitial,
charge, xInitial, yInitial))
muonTrack, muonPath = propagateMuon(angleInitial,
speedInitial, charge, xInitial, yInitial)
chamber1.getResiduals(muonTrack, muonPath)
# note the y and x axis are not to scale, meaning things are stretched. A tilted chamber will look shorter
if i%10000==0:
#plt.clf()
trackPaths = sub1.plot(muonTrack[0],muonTrack[1], color='orange', label="track")
muonPaths = sub1.plot(muonPath[0],muonPath[1], marker = 'o', color='red', label="actual path")
legend = sub1.legend()
plt.pause(simSpeed)
for muonPath in muonPaths:
muonPath.remove()
for trackPath in trackPaths:
trackPath.remove()
#plt.show()
#plt.draw()
#plt.clf()
learningRates = [50,10,1.5]
stepSizes = [.01,.01,.01]
for i in range(400):
shootMuons(chamber1)
#chamber1.alignGradDescent(learningRates, stepSizes)
chamber1.align()
#chamber1.align()
chamber1.resetData()
chamber1.cleanChamberPlot()
chamber1.plotChamber(sub1,sub2,sub3)
'''
xResidual = np.subtract( chamber1.hit[1], chamber1.track[1])
dxdyResidual = np.subtract( chamber1.hitXOverY, chamber1.trackXOverY)
plt.hist(xResidual)
plt.show()
plt.hist(dxdyResidual)
plt.show()
plt.scatter(xResidual,dxdyResidual, alpha=.1)
plt.show()
'''
我已经调试了很多东西,并且摆脱了很多错误信息。但是现在我已经到达运行multpleScattering.py的位置,但没有得到任何响应,该程序没有结束,但是我也没有得到响应。我认为这是由于我对线程的滥用,或者是由于我总体上缺乏将UI与multpleScattering集成的能力所致。再说一次,任何帮助都是令人敬畏的!