Adding GUI in tkinter to a simpy simulation

1.8k views Asked by At

I have a problem with implementing some GUI into my simpy simulation. The problem is how to add GUI, preferably made in Tkinter, to a simpy project. Wright now I have even problem with creating a window. I think that it's becouse when simulation stops running I can't exetue Tkinter code.. I know that this is a very general question, but I don't know how to start. What I want to do with Tkinter is to draw a topology for my simulation and maybe be able to make change some parameters, and run it from GUI.

I've added part when the simulation starts, and objects are created, the whole thing is more than ten times bigger so I don't know if i should add it here.

initialize()

network=Network()


#creating  nodes

node=Node(name='node', function='user', interfaceNum=2, possition=[160,990], homeAddress='0000000000000000020000fffe111111')
node.engine.stateTable={'wifiassocresp':'self.changeIp(interface, self.HO)','dissconnect':'node.engine.dissconnect(self.interruptCause, self.interruptCause.passSender)','RngRsp':'self.sendBU()', 'MIPv6BindingAck_LCoA':'self.sendBU(RCoA=True)', 'MihN2nHoCandidateQueryRsp':'self.makeHandover()'}
activate(node, node.start())

ap0=Node(name='ap0', function='ap', interfaceNum=2,possition=[200,1000])
ap0.engine.stateTable={'dissconnect':'self.dissconnect(self.interruptCause, self.interruptCause.passSender)'}
activate(ap0, ap0.start())

 ..... there is more nodes, but i cut it becouse I thinks that this is not important


activate(ha, ha.start())

ha.engine.HAadresses={'node':{ 
    'interfaceAddress':[node.interfaceList[0], node.interfaceList[0].address],
    'homeAddress':[node.homeAddress],}}


simulation=Tasks()


network.drawTopology(nodes=[node, ap1,ap2, map1, ap0, internet, ha])


allNodes=[node, internet, ap1, ap2, map1, ap0, ha]
node.apList=[ap0,ap1,ap2]

ap1.engine.createRouting([node, internet, map1, ap0, ap2, ha])
ap2.engine.createRouting([node, internet, map1, ap0, ap1, ha])
map1.engine.createRouting([node, ap1, internet, ap0,ap2, ha])
ap0.engine.createRouting([node, ap1, ap2, map1, internet, ha])
internet.engine.createRouting([node, ap1, map1, ap0,ap2, ha])


#here starts the simulation
activate(simulation, simulation.run(1))

simulate(until=100000.0)

And here is code for mu GUI

from Tkinter import *
from betaruch import *

class MainWindow:
    def __init__(self, master):
        ramka=Frame(master)
        ramka.pack()

        self.przycisk=Button(ramka, text="Wyjscie", fg="red", command=ramka.quit)
        self.przycisk.pack(side=LEFT)

        self.witam=Button(ramka, text="Uruchom", command=self.uruchomSymulacje)
        self.witam.pack(side=LEFT)        

    def uruchomSymulacje(self):
        pass

root=Tk()

onko=MainWindow(root)


root.mainloop()

And it's not suppoused to do anything, but it desn't even show up.

Ok maybe this will help. I've noticed that the problem occurs when I import both Tkinter and matplotlib. So maybe matplotlib is also using tkinter?

1

There are 1 answers

2
A. Rodas On

It looks like you are executing the code of your betaruch module in the same thread as your Tkinter code, so that is why your GUI is unresponsive. To solve this, you can execute the simulation in a new thread with the threading module:

import threading
from Tkinter import *
from betaruch import *

class MainWindow:
    # ...    

    def uruchomSymulacje(self):
        thread = threading.Thread(target=a_betaruch_function) # just a reference, without parentheses
        thread.start()